質問

When I crate a custom cell I create a class UITableViewCell and add a nib with a tablecell; it's very clear. I want to understand how to manage the situation when I should use this custom cell in some classes where I use a tableview.

For example I have a view controller called "FirstViewController" and inside this view controller I have a tableview, I import the class of my tableview cell and I create an IBOutlet for its reference

#import "MyCell.h"

IBOutlet MyCell *mycell;

In the file's owner of MyCell class in the identity inspector I set the class of FirstViewController, in this way I can link the IBOutlet of my custom cell easily.

In this way I can use MyCell only for FirstViewController, is there a way to share MyCell for other classes as a SecondViewController?

thanks

役に立ちましたか?

解決

You can create a subclass of UITableViewCell, say CustomCell. Create a xib file,add a UITableViewCell as root of xib. Change the identity of UITableViewCell as CustomCell. Connect outlets to the elements of CustomCell. But don't give ownership to any viewController.

Instantiate with following code in the tableView:cellForRowAtIndexPath: in any viewController you want

CustomCell *customCell = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" 
                                                      owner:nil 
                                                    options:nil][0];

他のヒント

"is there a way to share MyCell for other classes as a SecondViewController?" I would skip the IB case and add programmatically to the SecondViewController.

If you want to stick with IB, than you have to create a custom component, add to XCode, it is possible, but require more time to do it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top