Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS5 Storyboard: Reuse Custom UITableViewCell in multiple UITableViewControllers

Using a dynamic, custom cell prototype that I design in, say, UITableViewController A (in Interface Builder/Storyboard), works really well with dequeuing the cell (through its identifier, cellA) and such in cellForRow... I use a custom class (UITableViewCell subclass, let's name it MyCustomCell) to link up the labels and image thumb and it works all pretty well and straight forward in my UITableViewController A.

Now I create a UITableViewController B (in Storyboard), which happens to have the exact same design/functionality for its custom cells (dynamic cells). So I switch the class of these cells to the MyCustomCell and give it a new identifier, cellB.

In UITableViewController B, I dequeue the cell in cellForRow... and use the new identifier cellB. Note: Using cellA here leads to a crash, more or less obviously.

But when the table view shows up, while running the app, the UITableViewController A works just fine, and the almost identical UITableViewController B does not work (empty cells).

In Storyboard, it looks sort of off a bit, because the custom cell is designable within the UITableViewController A but in UITableViewController B, it's just a simple, plain cell. Despite the class associated to MyCustomCell.

How would one avoid copy&pasting these cells to the other controller (and therefore heavily going back and forth between copies when making design changes) – and rather just properly re-use it?

like image 974
James Stone Avatar asked Apr 12 '12 09:04

James Stone


2 Answers

What you are doing is correct. I don't know why its not working, it may be some problem with reloading the tableview; check with your datasource and the datasource method.

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section

rather what you can do is: drag a

Viewcontroller

and then drag a

tableview

and make it dynamic and do the thing that you did earlier; it worked fine for me when I did so myself.

like image 191
piyush Bageria Avatar answered Oct 10 '22 23:10

piyush Bageria


Saying the Cell is from a your custom class doesn't mean it's "designed" the same. What you really try to achieve here (and what I'm looking for) is some king of "Contained" Cell, but this is only doable with a ContainerViewController in iOS 6 I think. The other option is to use a XIB for that one Cell — that should work just as good, but then you lose the benefits of having an overview in the Storyboard.

like image 32
StuFF mc Avatar answered Oct 11 '22 00:10

StuFF mc