UISearchDisplayController changing row height

Steven Fisher picture Steven Fisher · Apr 14, 2010 · Viewed 8k times · Source

I've set my UITableView row height to in Interface Builder to 54.0. I have a UISearchDisplayController on that view. When the user taps the search bar in it, the table resizes properly. However, when they start typing (and actually doing the search) the row height decreases. It stays wrong until the search taps Cancel.

I could find no documentation on this behavior on Apple's site.

I've tried setting the row height in UISearchDisplayDelegate delegate calls. This might be the right approach, but I don't know the details and couldn't get it to work.

I've also tried implementing - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. This worked, but I have thousands of entries in this list and can't take the performance hit.

What's the right way to fix this?

Answer

Christopher Rogers picture Christopher Rogers · Feb 2, 2012

The correct way to do this is to use the following delegate.

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 54.0f; // or some other height
}

It's called when the UITableView is created or shown.

If you happen to call searchDisplayController.searchResultsTableView in other code when a search is not being performed, it will create the UITableView in advance. If you use the willShow delegate method to set the rowHeight, it will miss the timing (for some reason) to change the rowHeight if the tableView has been created beforehand.