0

I have a tableview with a SearchBar and delegate methods for it. I need to be able to display results from a SQL query obtained after using the search word into the search results as opposed to searching the tableview itself.

Getting the SQL query results is not a problem.

I just need to know how to directly populate the search result cells with the text from my query.

All of the examples I have found seem to deal with filtering the original tableview with data matching the search term so they don't help in my situation.

Can someone provide an example or code I could put in the searchBarTextDidEndEditing delegate to fill the search result with data from an Array for example.

I tried doing something like this, but it is obviously not working and I am still trying to learn IOS programming so I don't have any experience working with searchBars and may be out in left field here...

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"search bar did end text editing %@", searchBar.text);
    _searchResultsCellData = [[NSMutableArray alloc] init];
    NSMutableArray *searchCells = [[NSMutableArray alloc] init];

    NSDictionary *cellSearchContainer = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"TEST", @"location", @"", @"", @"", @"2", nil]
                                                                forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [searchCells addObject:cellSearchContainer];

    [_searchResultsCellData addObject:searchCells];

    static NSString *CellIdentifier = @"parentCells";

    UITableViewCell *cell = [_parentsTable dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.textLabel.text = @"test";

}
FlashSolutions
  • 85
  • 1
  • 10
  • It's no different than how you populate the main table view. After doing the query you create appropriate data structures to hold the data. Then all of the table view data source methods need to reference the search results data structures instead of the main table view data structures. – rmaddy Feb 17 '14 at 00:08
  • So assume I have the data structures to hold the data. What data source methods specifically are you referring? I am still trying to wrap my head around where this result view lives. I don't want to change the main table view data, only the result view of the search bar. – FlashSolutions Feb 17 '14 at 00:23
  • Are you using a `UISearchDisplayController` (you should be)? – rmaddy Feb 17 '14 at 00:30
  • Yes, I am using the UISearchDisplayController. – FlashSolutions Feb 17 '14 at 00:31
  • OK, then you should have set the `searchResultsDataSource` and `searchResultsDelegate` properties. And most likely these are set to your table view controller. In all of your table view data source and delegate methods in your table view controller, you need to look at the `tableView` parameter. If it's `self.tableView` then use the main data structure otherwise use the search data structure. – rmaddy Feb 17 '14 at 00:36
  • I put an If statement in cellForRowAtIndexPath to check if the tableview was my main view (which is named _parentsTable) and an NSLog statement if if were not. It never reaches the else statement. So my if statement reads If (tableView == _parentsTable) ... Am I on the right track? If so, why would I not reach the else clause. – FlashSolutions Feb 17 '14 at 00:45

0 Answers0