Search code examples
user-interfaceuitableviewuistoryboarduisearchdisplaycontroller

Getting multiple table view instances to match


I'm trying to get the cells returned within my searchResultsTableView to look just like those in the "regular", non-search table view. Here's my regular view.

The first time you search, the results are styled exactly like the "regular" table view. However, once you you cancel the searchDisplayController and tap the Search button again, you'll get this.

It looks to me like the background isn't being set after the first time the search display controller is loaded.

I'm instantiating cells in tableView:cellForRowAtIndexPath: using UITableViewCell, not a custom subclass. Here is the remainder of the method:

PersonInfo *info = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
    info = [self.filteredPeopleList objectAtIndex:indexPath.row];
} else {
    info = [self.peopleList objectAtIndex:indexPath.row];
}

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBk.png"]];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor clearColor];

cell.imageView.image = info.image;
cell.textLabel.text = info.name;
cell.detailTextLabel.text = info.title;

return cell;

The tableView (the "regular" one) and searchResultsTableView is being set as follows inside viewDidLoad:

//Set the default tableView style
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

//Set the self.searchDisplayController background stuff
self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor clearColor];
self.searchDisplayController.searchResultsTableView.opaque = NO;
self.searchDisplayController.searchResultsTableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[self.searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

[self.tableView reloadData];

Solution

  • Moving the code to style the searchResultsTableView into searchDisplayController:willShowSearchResultsTableView:, a method of UISearchDisplayDelegate, takes care of this.