0

I've been searching for this day, but I can't find it....

Could anyone point me to a tutorial/tell me what I should do? The documentation wasn't really helpful... I need this for my UITableViewController class (without a xib!)

Thanks A lot!

Martin Herman
  • 888
  • 13
  • 34
  • 1
    Have you seen this ? http://stackoverflow.com/questions/2959299/gray-uisearchbar-w-matching-scope-bar-programmatically – Janak Nirmal Aug 09 '11 at 10:50
  • interesting... but: where do I add what should be displayed in the "search table view"? I know the datasource is self... but I don't know what to do further.. – Martin Herman Aug 09 '11 at 11:38
  • 1
    Implement the Search Bar Delegate Methods and when the User types, pick out the matching rows from your old Data Source and create a new one and reload your table with the contents of the new Data Source. – Suhail Patel Aug 09 '11 at 13:54

1 Answers1

0

First do Gray UISearchBar w/matching scope bar programmatically

Then since the tableview and the searchtableview both use the same datasource, you have to insert an if statement in the tableview delegate methods like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResutsTableView) {
        tableViewData = searchResultsData objectatindex...; //array with filtered data
    } else {
        tableViewData = defaultData objectatindex...; //array with unfiltered data
    }
}

Do the same for the number of rows delegate method:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return searchResultsData.count;
    } else {
        return defaultData.count;
    }
}

Then after the search text changes or search button is pressed (look at UISearchBar delegate methods), reload data to display.

Community
  • 1
  • 1
chourobin
  • 4,004
  • 4
  • 35
  • 48