views:

364

answers:

1

Hi,

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?

+1  A: 

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.

TechZen
Hmm :/ I keep getting an error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:"Here is my cellForRowAtIndexPath code - http://pastie.org/867839And my XIB - http://drp.ly/AB68l
Jamie Maddocks
The custom cell needs to be in the nib/xib for the view controller. You've got it in its own nib and you've set cell class the file owner of that nib. The method that sets the cell for the table is in the view controller. You need to move the tableviewcell in the nib to the nib for the view controller and then connect the self.myCustomCell outlet to it.
TechZen