Jim
Jim

Reputation: 9234

iPhone: How to use UITableViewCell in two table views

I have 2 classes that inherit UITableViewControllers. And this two table views should use the same custom UITableViewCell. So how can I use a Custom UITableViewCell from a Nib File in two different classes ? The owner of the xib can be only 1 class.

@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;

Upvotes: 5

Views: 731

Answers (3)

rakeshNS
rakeshNS

Reputation: 4257

Create a UITableViewController sub-class say CommonTableView. Make this as the owner of nib file. Then inherit the two classes you want to implement from CommonTableView Class. This will work fine.

Upvotes: 3

lawicko
lawicko

Reputation: 7344

Use UINib class. It allows for instantiation without parent, and also improves the performance by caching nib file contents.

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

... and the similar thing for Class2. 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]; 

Upvotes: 0

Mehul Mistri
Mehul Mistri

Reputation: 15147

Based on my opinion , You should have to create Custom cell and and for other table you should have to create only XIB for that and use .h and .m file same...

just change controller in XIB...

Hope you understand..

See this How to create Customcell and this

Upvotes: 0

Related Questions