Vasily Vasily - 1 year ago 77
iOS Question

Show cell outside UITableView

I have problem that was asked here UITableViewCell outside of UITableView but has no correct answer except ‘it’s not a good idea’.

I want to load cell’s contentView outside tableView (example situation — when user’s click cell he is navigated to new table view where that cell acts as part of headerView).

Resolution proposed in topic I’ve provided link was:

What I have done is loaded the cell from its nib, set it up, and then
added the contentView of the cell to my new pushed view. This achieves
much of the same effect.

I’ve tried to load cell via
MyUITableViewCell(style:, reuseIdentifier: nil)
but every time when I try to call any object inside cell (e.g. button) I receive crash that object is
, so that’s means that cell isn’t loaded from nib.

I can’t initialize cell via
MyUITableViewCell(nib: nib, bundle: nil)
, because it hasn’t such initializers.

So how can I load cell’s content in my any other view? How can I ‘set it up’?

Answer Source

To straight up load a cell from a nib without using the table view, you need to load it from the main bundle.


CustomClass *cell = (CustomClass *)[[[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil] firstObject]


let cell = NSBundle.mainBundle().loadNibNamed("nibName", owner: self, options: nil).first as! CustomClass

Once you load it you still need to do your setup and set the frame of the cell and add it to superview. You can then use this view just like any other view.

The reason why the init methods don't work is because the view never gets any of its IBOutlets initialized from the nib, so all your cell's properties for the outlets are nil.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download