Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchDisplayController changing row height

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?

like image 648
Steven Fisher Avatar asked Sep 11 '25 04:09

Steven Fisher


1 Answers

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.

like image 52
Christopher Rogers Avatar answered Sep 12 '25 19:09

Christopher Rogers