0

I am working on an app that has several prototype cells in one view. This worked well for easily altering the appearance of the app while in development using the storyboard. However, now I'm adding search (filtering) capability. I would like the appearance of the tableview to remain unchanged, just filter out some of the results.

My understanding is that I have to create new cells to do this. Is this correct? If it is, is there a way to create a cell with all the properties of my prototype cells. As it is now, the newly created (search result) cells have default settings.

Thanks.

erosebe
  • 927
  • 3
  • 16
  • 31

2 Answers2

1

The thing to understand clearly is that the table view that appears when you are doing a search with the UISearchDisplayController conglomerate is not your table view. It is a different table view, and you do not have a UITableViewController managing it - the UISearchDisplayController does that. Thus you must take other measures if you want that different table view to look like your table view.

EDIT: On the whole (and after the little exchange with rdelmar in the comments on his answer), I tend to think the easiest solution is to abandon the use of cell prototypes altogether. If you design the cell in a nib (xib), you can then use that cell both for the real table and for the search results table. In both cases you register the nib with the respective table view - and then dequeue just does the right thing all by itself, in both cases, with no change in the code.

You can see me doing something similar here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch21p632searchableTable/p536p550searchableTable/RootViewController.m

... except that in that case I'm registering the same cell class for both tables, not the same nib. But it all comes down to the same thing. However, note that I do not start with a storyboard, so I never fell into the trap of using a prototype cell in the first place.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yeah. So, I have to programatically alter this new search results table view to have all the features of my storyboard created table view. Was hoping for a slick way around that. – erosebe May 10 '13 at 16:14
  • Here is a slick way that I suggest you *not* use: http://stackoverflow.com/a/11633838/341994 You see what he's trying to do: he's drawing cells for the search results table from the original table, thus passing thru the storyboard to fetch copies of the prototype. I really can't recommend that! – matt May 10 '13 at 16:18
1

You can certainly use copy and paste. Create a xib file (an empty one), and copy the cell you want from your table view in the storyboard, and then paste it into the xib file. In the viewDidLoad method for your table data source, register that nib file:

[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"SearchCell"];

Then in the cellForRowAtIndexPath method, you just dequeue a cell with that identifier for your search results table view.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Yes, and I was going to point out that this is one of the reasons I don't like storyboards. This whole problem would have been avoided if the OP's cell had come from a nib (not a storyboard prototype) in the first place. In that case, the cells for the original table and the cells for the search results table could be obtained in exactly the same way as one another. – matt May 10 '13 at 16:29
  • @matt, yes, in this particular case I would agree with you, though for me personally, it doesn't out weigh the ease of using a single storyboard vs. multiple xibs for a project with many controllers. I like the ability to see the whole flow of the app in one place. – rdelmar May 10 '13 at 16:34
  • I'm not saying that the OP should not use a storyboard at all. But, you see, the storyboard then invites you to use a prototype cell, and so we fall into this trap. It is perfectly possible to use a storyboard to instantiate the table view, yet draw the cells from a nib. And if you do that, it becomes trivial to obtain a matching cell for the search results table! But the storyboard lulls one into using a prototype instead, thus making things *harder* if we want to make the table searchable. – matt May 10 '13 at 16:43
  • Stupid apple. It's all their fault. I'm the **victim** here. :) Thanks for all your help guys. – erosebe May 10 '13 at 16:57