Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I found it!

Assuming the table is stored in tableView:

- (void)viewDidLoad;
{
    [super viewDidLoad];
    self.searchDisplayController.searchResultsTableView.rowHeight = tableView.rowHeight;
}

Nothing else is necessary.

Edit: See netmikey's answer for a better solution.

Steve, your solution didn't work for me: The first time the search result would display fine, but when a user was hitting "Cancel" (closing away the search bar) and then reopening it and entering a search term into it, the height would be wrong again.

DigDog's solution of using searchDisplayController:didShowSearchResultsTableView was kinda working, but users saw the cell height "jumping" when the search started.

The solution I found, that fixed both these issues is using:

- (void)searchDisplayController: (UISearchDisplayController *)controller 
 willShowSearchResultsTableView: (UITableView *)searchTableView {

    searchTableView.rowHeight = myTableView.rowHeight;
}

... in the UISearchDisplayDelegate.

Regards, netmikey

In viewDidLoad:

you can set the search display controller's delegate to self:

- (void)viewDidLoad 
{
    self.searchDisplayController.delegate = self;
}

Then, implement the table view delegate in your view controller:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableview == self.searchDisplayController.tableView) {
        return 55;
    } else if (tableView == self.tableView) {
       return  44;
     }
}

Overriding the method didLoadSearchResultsTableView should do the trick.

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
    tableView.rowHeight = 82.0; // Change height of search result row
}

More explanation from this blog

You need to set both

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    
    return 54.;
}

and

self.tableView.rowHeight = 54.;

also in your UISearchDisplayDelegate

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
    tableView.rowHeight = 54.; 
}

Otherwise, when "No Result" happened in searching, cell height will fall back to default.

I've been using ios 7.0 and Xcode 5.0. I think you should change you row height in this method:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        tableView.rowHeight = ...;// change your row height here!
        return [self.searchResults count];
    }
    else
    {
        ...
        return ...;
    }
    }

Because every time a tableview try to display its layout, this method is called, so everytime you type a letter into search bar, this method is called,and the row height won't have a chance to change.

None of the solutions worked for me...

Did my own hit and trial... and got it working...

If you want different row heights for normal & search table views...

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// for normal results
     if (tableView==self.tableView) return 54;
// for search results
     else return 54; }

And if you want common height...

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return 54; }

PS: Do keep in mind, if you have same row height for each row, this method is not very efficient resource usage wise, as the row height is re-calculated for each cell... Although, this is unnoticeable in most cases, I am still searching for a better solution!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top