0

What's the simplest way to make UISearchDisplayController display exactly the same cells (formatting, height, fonts, colors, etc) as the original tableView that it searched on? or simply put - make it dequeue de same cell identifier?

I am using a the standard subclassed UITableViewController + UISearchDisplayController solution, and prefer to stick to it.

thanks

mindbomb
  • 1,642
  • 4
  • 20
  • 35

1 Answers1

2

I actually found the simplest way.

All I had to do is change the default cellForRowAtIndexPath second line of code and add the "self." to tableView to the dequeue cell identifier - this way the the cell style which is in the storyboard always gets dequeued and not the (non-existant) one from the searchResultsController.tableView

Also implement heightForRowAtIndexPath that will return the same height without any check (searchresults table or self.table - we want the same height)

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PhraseCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //                       ^^^^ NEW

    //...
mindbomb
  • 1,642
  • 4
  • 20
  • 35
  • It seems slick but it's just wrong. Drawing cells for table B from table A's reuse pile is just not supported. Among other things, you're subverting table B's reuse pile - *none* of its cells are now being reused. So a table view with, say, 1000 rows would now need 1000 cells, instead of the usual 8 or 10 maximum which reuse gives you. – matt May 10 '13 at 16:31
  • @matt I totally agree with you, I thought (think) so too, but I took a look at Apple's source code [here](https://developer.apple.com/library/IOs/samplecode/TableSearch/Introduction/Intro.html). Would you mind commenting on whether it could be that Apple managed the reuse piles to be shared somehow? I know Apple does not always provide 100% correct source code, but if I look at all the answers to that question on SO they all say to use `self.tableView` ... What do you think? – HAS Sep 10 '13 at 15:11
  • @HAS I see why it works. They are not exactly shared, but the point is that there is no disaster if there are 1000 rows in the search results table because cells are still released when they scroll out of sight. Thus there are always just 9 or 10 cells in existence at one time, and that's all that matters. I take back my criticism! Thank you for getting me to look at this again. – matt Sep 10 '13 at 18:11
  • @matt Thank you so much for your feedback! :) Now I can do it with a clear conscience! :D – HAS Sep 10 '13 at 19:03