IPhone: how to use UITableViewCell in two table views

I have 2 classes that inherit from UITableViewControllers. And in these two table views, the same custom UITableViewCell should be used. So, how can I use a custom UITableViewCell from a Nib file in two different classes? Only 1 class can be the owner of xib.

@interface Class1 : UITableViewController<UITableViewDataSource,
    UITableViewDelegate> {
UITableViewCell *myCustomTableRow;
}

@property (nonatomic, retain) IBOutlet UITableViewCell *myCustomTableRow;





@interface Class2 : UITableViewController<UITableViewDataSource,
    UITableViewDelegate> {
UITableViewCell *myCustomTableRow;
}

@property (nonatomic, retain) IBOutlet UITableViewCell *myCustomTableRow;
+5
source share
3 answers

Create a subclass UITableViewController, say CommonTableView. Do this as the owner of the nib file. Then inherit the two classes that you want to implement from the class CommonTableView. This will work fine.

+3
source

, , XIB .h .m, ...

XIB...

, .

Customcell this

0

Use the UINib class . It allows you to create instances without a parent, and also improves performance by caching the contents of the nib file.

@interface Class1 : UITableViewController<UITableViewDataSource, UITableViewDelegate> {
    UINib *myCustomCellNib;
}

... and a similar thing for class 2. Then in viewDidLoad

myCustomCellNib = [UINib nibWithNibName:@"myCustomCellNibName" bundle:nil];

then when creating the cell:

NSArray* objects = [myCustomCellNib instantiateWithOwner:nil options:nil];
// given that your cell is defined as a first object in your nib
cell = [objects objectAtIndex:0]; 
0
source

All Articles