UISearchController Tutorial: Getting Started

Thanks for the tutorial! It’s great! I have got it :smile:

PS: It will be better if user can scoping the list directly by taping the scope titles, without typing anything into the searchBar :smile:

I am trying to make this happen, thanks for your sharing :smile:

Hi Andy. Thank you for this tutorial. It helped me a lot.

  • I really liked that it is short and a good getting started tutorial.
  • What I really missed is going a bit deeper into knowing UISearchController/UISearchbar.

I have updated the project to Swift 3.0 with XCode 8.2.1.
https://github.com/Darkwonder/RW-UISearchController-Tutorial-Getting-Started

@lupo & @smallgreymouse If you look at the UISearchBarDelegate extension, there is searchBar(_:selectedScopeButtonIndexDidChange:). This is called every time you change the segment. However, since the current example is passing this and text to filterContentForSearchText(_:scope:), it doesn’t filter the way you’re wanting. You could easily write some more logic to handle the scenario that no text is passed, and filter the candies based on scope only. Good luck!

Thanks @darkwonder! Always nice to here it helped.

thank for this awesome tutorial post !!

Hi Darkwonder, I just run your project but the search function doesn’t work anymore…

Hi Iris. Thank you for the update and check. I have corrected the bug. I tested it and it worked.

Try not to force unwrap any of your objects. You may be getting an error regarding the item name. Instead something like this may eliminate the error.

filteredResult = result.filter { item in 
   guard let name = item.name else { return "" }
   return name.lowercaseString.containsString(searchText.lowercaseString)
  }
  listTableView.reloardData()
}

Hello mark i have successfully implemented search items thank you very much. but i want my results to be sort depending on a occurrences of the search text into categories, how do i do that?

currently we are using following method to search what changes i need to do in this, i have tried different things though no luck
here is the code

func filterContentForSearchText(searchText: String, scope: String = “All”) {
filteredCandies = candies.filter { candy in
let categoryMatch = (scope == “All”) || (candy.category == scope)
return categoryMatch && candy.name.lowercaseString.containsString(searchText.lowercaseString)
}

tableView.reloadData()
}

thanks.

I have updated the project to Swift 3.1 with XCode 8.3.2.

Hello, thanks for the tutorial! I’m having a slight bug in my project and was wondering if this was an issue anyone else had encountered. There is a blank white space under my search bar. hofefully i can explain it better with photos. it appear after the search bar is tapped and before its tapped. Thanks!