views:

9379

answers:

2

Implementing a searchbar can be tricky business and I'm struggling to make it work for my situation. Here's a collection of some of the sample code I've found for implementing a UISearchBar. Please share any others below you've come across below.

Apple's TableSearch code implements a contacts-like search of a simple tableview.

Beginning iPhone Dev Book's Sections sample code implements a searchBar on a sectioned tableview. Data is pulled from a plist.

iPhone Developers Cookbook Chapter 8, 10a SearchBar example searches a table of color names. Selection of a color name changes the tint of the search bar. Also see their Chapter 9 01a-Finding People app.

iPhoneSDK's source code TableViewPart6 searches a table of country names with 2 sections for countries visited and those to visit.

Stanford's iPhone app programming class's 08-MyTableView sample filters a simple list of names.

+4  A: 

You can check this tutorial out:

http://www.iphonesdkarticles.com/2009/01/uitableview-searching-table-view.html

Corey Floyd
Thanks! Although this is the tutorial for one of the sample code links posted above.
Jonah
I won't downvote it, because it's accurate, but no one targeting SDK 3.0 and higher should consider this approach. See the answer below.
Justin Searls
+7  A: 

WAIT: Apple has implemented the Search Display Controller in the iPhone SDK 3.0. You can drag that to the header of your tableView, or programmatically set that as headerView. You only have to set a few simple parameters (like the dataSource, which is just an UITableViewDatasource and a viewController, where the results have to been showed in). This gives you exactly the same look as the contacts-application and all other Apple-applications (you can also set 'scopes' like the Mail-app)

Tim van Elsloo
The new Search Display Controller is great unless you want to create a more custom design for the searchbar. In my project, I have a button that makes the searchbar appear. I haven't figured out a way to use the Search Display Controller with a search bar that is added programmatically so far.
Jonah
The searchbar which is made by the UISearchDisplayController is also just an UISearchBar and can be called by [yourSearchDisplayController searchBar]
Tim van Elsloo
Thanks, I'll look into it.
Jonah