The UITableViewCell is great for laying out information to be displayed in a UITableView (obviously), but there doesn't seem to be a simple way of getting the NIB you have created within the Interface Builder and your UITableViewCell. Thankfully, it's quite simple.

Create your UITableViewCell



@interface MyTableViewCell : UITableViewCell {
IBOutlet UILabel *exampleLabel;
}



And add a layoutSubviews method within the implementation section; ie -


- (void)layoutSubviews {
exampleLabel.text = @"This is my example!";

/*
You may also have a little more control over your font here also
*/
exampleLabel.font = [UIFont boldSystemFontOfSize:12.0];
}


Now, create a new empty XIB file and call it whatever you want - I will use MyTableViewCell to match the Obj-C class. Open this XIB up using Interface Builder and look at the two items in the doc window: File's Owner and First Responder. In the inspector, change the File's Owner class to be UIViewController if this isn't already set. From the library, drag a Table View Cell into the doc window after the First Responder. Change the class of this new Table View Cell to be MyTableViewCell. Now associate the main view with the Table View Cell by clicking on File Owner and then on the Connections Inspector on the entry for 'view', drag a line from the empty circle at the end of this line onto the 'My Table View Cell' entry in the doc window.

OK, now let's set up the label. Double click on the 'My Table View Cell' entry in the doc window. Drag a label from the library into the view of 'My Table View Cell' that should have been opened. Within the outlets group in the connections inspector for the table view cell, drag a line from the circle at the end of the 'exampleLabel' line onto the label we just added to the table cell. Save the view.

That's all great, but we haven't yet told our code how to load up the XIB for this table cell view yet. We need to have a view controller conforming to the UITableViewDelegate and UITableViewDataSource protocols (which we won't go into here). Within the '- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath' method, we need to create our UITableViewCell implementation.



- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyTableViewCell";

MyTableViewCell *cell =
(MyTableViewCell *)[inTableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
UIViewController *c = [[UIViewController alloc]
initWithNibName:@"MyTableViewCell" bundle:nil];

cell = (MyTableViewCell *)c.view;
[c release];

// code here to set whatever values in MyTableViewCell
}

return cell;
}



Obviously this is a pretty basic example, but the code follows the same principle for all other elements you may wish to add to your UITableViewCell.