searchDisplayController not updating UITableVIew

258 views Asked by At

I have a UITableView with a Searchbar & Search Display Controller. My code appears to be correct but when I type in the searchbar, I get no results. The UITableView remains the same.

My segue is correct as it is still passing data correctly as well to the next VC just fine.

A push in the right direction would be appreciated.

**Note: I realize that searchDisplayController is deprecated starting with iOS8 but I want to figure this out for the sake of knowing.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];

} else {
    return [bands count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"BandCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [bands objectAtIndex:indexPath.row];
}

return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:[self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}

EDIT

connections

I noticed that searchBar wasnt connected. When I connected this, I noticed that the searchbar would dissapear whenever I would tap inside it. It looks like the searchbar is dissapearing behind the navigation bar. Thats another issue for later.

I am still unable to get the table to update correctly.

Source: Source

EDIT

On a side note, I figured out why my searchbar was dissapearing. I had my tab bar embedded in a navigation controller when I should have had my navigation bar embedded in the tab bar.

So: Tab bar --> Navigation controller

Adding the following code stopped my searchbar from moving up and overlapping the status bar

self.definesPresentationContext = YES;

EDIT

Not sure why but I noticed that my searchbar / search controller was not wired correctly. I removed it and added it back and everything wired up correctly. Specifically:

missing connection

searchDisplayController was not wired up to Band list.

3

There are 3 answers

0
Martin On BEST ANSWER

All in all my code was correct. My searchbar/search controller were not wired correctly. Removing it and adding it back connected everything up correctly. There is more information in my original post.

0
ashForIos On

Does this method called i.e the delegate method of searchDisplayController ---

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

        [self filterContentForSearchText:searchString
                                   scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                          objectAtIndex:[self.searchDisplayController.searchBar
                                                         selectedScopeButtonIndex]]];
        return YES;
    }

and whether you data in this line

 searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
0
Yuyutsu On

Replace this below method. It may helps you

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
[self.tableView reloadData]; //add tableview reloadData
}