Balvansh Heerekar
Balvansh Heerekar

Reputation: 85

How to show suggestions while searching in iOS?

I am working with Swift 3. I am basically using an API to search for images from the web. I want to add suggestions to my searchBar when any word is being entered. For example when I search "Sa", I get suggestions like "Sand", "Samsung", "Sail", etc. Any suggestions as to how I can implement this?

The output I get after executing the query is in JSON format.

I want my Search Bar to look something like this

Upvotes: 0

Views: 4182

Answers (2)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

Try like this to search i am searching in array of dictionary with name i show the all result search in my tableview below the UISearchBar

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
    let namePredicate = NSPredicate(format: "name contains[c] %@", searchText)
    if HotelData.count > 0
    {
        searchData = (HotelData as NSArray).filtered(using: namePredicate) as [AnyObject]
        tlbSearch.reloadData()
    }
}

Upvotes: 0

Nef10
Nef10

Reputation: 946

There are multiple ways.

You can use the KeyboardAccessory. The shows a view over the keyboard (where auto complete is normally places) which you can use this way you like, e.g. add three buttons which will trigger the input of the correct suggestion. Apple provides an example here: https://developer.apple.com/library/content/samplecode/KeyboardAccessory/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009462-Intro-DontLinkElementID_2

Another way is just by placing an view (most likely table view) on your current view, place just below the input filed, either with a fixed height of e.g. 3 items or till the keyboard. For this approach you can have a look into this tutorial: https://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values

Upvotes: 1

Related Questions