1

I'm implementing a UISearchDisplayController in my iOS 7 & 8 app. I'm using Storyboards to build the interface, and I've successfully added the search controller to my table, and everything is hooked up fine, to the extent that searching works just fine.

My issue is that the table rows have been configured using Auto Layout to provide dynamic row heights based on the contents of the cells. While the layout is fine in the regular table, those constraints are not applied in the search results table. Here's some pretty pictures to show what I mean.

Left: Normal table view; Right: Search Results table

Every tutorial and bit of documentation assumes the results table will have a static height, and the advice is to use tableview.rowHeight or return a float from tableView:heightForRowAtIndexPath.

In the cellForRowAtIndexPath method, I'm dequeuing my cell from the regular tableview like so:

ScheduleTableViewCell * cell = [self.scheduleTable dequeueReusableCellWithIdentifier:@"cell"];

I would have expected this to use the cells as I've laid them out in the Storyboard, but that apparently doesn't include the constraints.

Is there some method I can kick to make this happen? Or do I need to manually determine each row's height like I did back before AutoLayout? Like some kind of filthy animal?

Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75
  • Are you using the cell you created in the storyboard for both the regular table and the search results table? – rdelmar Oct 02 '14 at 18:48
  • Yes, that's what I'm trying to do. – Aaron Vegh Oct 02 '14 at 18:52
  • Are you using the new self-sizing cells introduced in iOS 8 (as in setting the table view's estimated row height and rowHeight to UITableViewAutomaticDimension)? If so, have you done that for both tables? – rdelmar Oct 02 '14 at 19:00
  • Holy cow. Using `return UITableViewAutomaticDimension` for heightForRowAtIndexPath did it. Thanks @rdelmar! If you phrase this as an answer, I'll mark it correct! – Aaron Vegh Oct 02 '14 at 19:31
  • I tried the same, but its not working...I have a UILabel inside my tableview thats variable height. I have set both searchableview and regular tableview to UITableViewAutomaticDimension. While regular tableview works fine, search does not. – cableload Oct 08 '15 at 03:02

3 Answers3

3

When using self-sizing cells, you should set the rowHeight property of both tables to UITableViewAutomaticDimension, and set the estimatedRowHeight for both tables as well. You can do this in viewDidLoad, there's no need to implement heightForRowAtIndexPath.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

As per @rdelmar's answer I wanted to add. For ppl who were confused just like me you should set both properties as follow:

// for the main tableView
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100 //or whatever height you prefer

// and for searchDisplayController
self.searchDisplayController?.searchResultsTableView.rowHeight = UITableViewAutomaticDimension
self.searchDisplayController?.searchResultsTableView.estimatedRowHeight = 100 //or whatever height you prefer

P.S. Sorry I can't add comment so I'm writing an answer

0

I struggled over this the last few days as well and unfortunately rdelmar's answer did not fix the issue for me, so here is what I had to do to solve it:

Instead of:

self.searchDisplayController.searchResultsTableView.rowHeight = UITableViewAutomaticDimension;

I had to use:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewAutomaticDimension;
}

This only got me half way there. There was also an underlying issue with my constraints in the tableViewCell. My textView needs a dynamic height, and so I provided no height constraint at all for the textView. It resized appropriately based on it's constraints to the views above and below it. This worked great in my original tableView, but not so in the searchResultsTableView.

I had to add the height constraint to the textView and use a >= (greater than or equal to) constant. Finally the searchResultsTableView mimicked my original tableView.

Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Frankie, I set the height constraints for my tableview (>= like you mentioned), but now instead of expanding, the uilabel gets cut off... – cableload Oct 08 '15 at 03:04