2

I have table view with a Search Bar above it. My intention is to have users enter a query in the search bar and have the table view populate with results - either when the user presses enter or as they're typing.

After reading a number of tutorials, I selected the Search Bar and Search Display Controller for the Search Bar. However, it seems this controller is less of an enter-query-then-display-results tool than a filter-existing-table-view-data tool. This means I'm coming upon a table view that already has all the data and is filtered as I type -- what I'd like is to come upon an empty table view and have it populate.

I was wondering if there was a way to use the Search Bar and Search Display Controller to achieve the effect I want or it there was a preferred way?

H K
  • 1,105
  • 2
  • 14
  • 27
  • So what do you want to show before you do a search? Just a search bar without a UITableView? – ansible Oct 10 '14 at 18:31
  • I'd like to display and empty table view with a search bar above it. – H K Oct 10 '14 at 18:32
  • Use UISearchController, which is new in iOS 8. It's much better than what came before. – matt Oct 10 '14 at 18:50
  • @matt is there a preferred way of using the new UISearchBar? i'm confused because when i drag in a 'Search Bar and Search Display' object, it automatically is a deprecated UISearchDisplayBar object – H K Oct 12 '14 at 18:36
  • See my github site for three different examples (four, actually): https://github.com/mattneub/Programming-iOS-Book-Examples - you want the ones called SearchableTable. – matt Oct 12 '14 at 19:19
  • Hi @HarlanKellaway I've been searching about what I want to do and your question is exactly that. Could you share with me your coding sample as a guideline? What I've done is in [this question](http://stackoverflow.com/questions/29313349/search-in-pfquerytableview-not-working). Thank you very much. – SanitLee Mar 29 '15 at 10:05
  • 1
    @sanitlee I actually ended up going with the other type of search bar - just an independent search bar and an independent table view that is populated by the search bar's delegate – H K Mar 29 '15 at 18:01
  • @HarlanKellaway would you mind to share your code for this part with me as an answer to [my question here](http://stackoverflow.com/questions/29313349/search-in-pfquerytableview-not-working)? That would help me out finally. Please. – SanitLee Mar 29 '15 at 18:11

3 Answers3

2

These two Ray Wenderlich tutorials are great for learning how to implement search into your UITableViews.

This tutorial covers the basic Search Bar with Objective-C. This tutorial covers the Search Bar using Swift.

Basically (very basic implementation level here) you will want to know if you are searching or not. If you are not searching, in your tableView:numberOfRowsInSection: method you can return 0, otherwise return the count of the results. Then in your tableView:cellForRowAtIndexPath: method you can customize the cell that is being displayed based upon the results.

c_rath
  • 3,498
  • 2
  • 19
  • 14
1

When using UISearchDisplayController you'll have two UITableViews. The one in your search view controller. So assuming you are hooking up both UITableView's dataSources to your UIViewController, just check which table is being passed in and return nothing if it's not for the search.

For example

- (NSArray *) _sectionArrayForTable:(UITableView *) tableView {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        // Return your search results
    }

    // Show nothing when not searching
    return 0;
}
ansible
  • 3,561
  • 2
  • 16
  • 29
  • That's it - the 'return 0'; was what i was overlooking. Thank you! – H K Oct 10 '14 at 19:03
  • 1
    self.searchDisplayController.searchResultsTableView is deprecated in iOS 8 – venky Oct 11 '14 at 17:03
  • @venky is there a preferred way of using the new UISearchBar? i'm confused because when i drag in a 'Search Bar and Search Display' object, it automatically is a deprecated UISearchDisplayBar object – H K Oct 12 '14 at 18:36
  • @HarlanKellaway This may helpful for you http://stackoverflow.com/questions/25826332/searchdispalycontroller-deprecated-ios-8 – venky Oct 14 '14 at 14:00
0

When the text changes, you will want to determine which results to show for that string and update some array or dictionary. Implement the tableview datasource methods to show the contents of that array/dictionary and call reload table when the text changes

Zach
  • 1