Jamie Maddocks
Jamie Maddocks

Reputation: 73

iPhone: How to Define and Use a Custom UITableViewCell from a Nib File

I'm trying to create a single custom UITableViewCell from a xib, among other normal UITableViewCell's. I've tried quite a few varied things with no success. Can anyone help me please?

Upvotes: 1

Views: 1028

Answers (1)

TechZen
TechZen

Reputation: 64428

The simplest method is to create an outlet for the tablecell in your tableViewController and then wire that outlet to the custom cell in Interface Builder. Place the cell in the nib of which the tableViewController is the File Owner.

So the definition would look like

IBOutlet UITableViewCell *myCustomCell;
...
@property(nonatomic, retain)  IBOutlet UITableViewCell *myCustomCell;

and to use it, you would:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ....
    (some condition test)
    cell=self.myCustomCell;
    (configure cell)
    return cell;
}

I've squeezed dozens of custom cells into a nib with no problem. The technique is especially useful when your creating a preference style table in which every cell is unique.

Upvotes: 1

Related Questions