I subclassed the UITableViewCell in order to customize it, but I think I\'m missing something because: 1) It\'s not working and 2) There are a couple of things I\'m confused
You have to load the cell from the .xib instead:
if ( cell == nil ) {
cell = [[NSBundle mainBundle] loadNibNamed:@"CellXIBName" owner:nil options:nil][0];
}
// set the cell's properties
The easiest way (available since iOS 5.0) to create a custom table view cell in a nib file is to use registerNib:forCellReuseIdentifier:
in the table view controller. The big advantage is that dequeueReusableCellWithIdentifier:
then automatically instantiates a cell from the nib file if necessary. You don't need the if (cell == nil) ...
part anymore.
In viewDidLoad
of the table view controller you add
[self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"];
and in cellForRowAtIndexPath
you just do
CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"];
// setup cell
return cell;
Cells loaded from a nib file are instantiated using initWithCoder
, you can override that in your subclass if necessary. For modifications to the UI elements, you should override awakeFromNib
(don't forget to call "super").
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CueTableCell";
CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [array objectAtIndex:0];
}
return cell;
}