I’ve a UITableViewController with a lot of cell, but they all have the same configuration — it’s only the cell content which differs. Here is my tableView:cellForRowAtIndexPath: method in my UITableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WSTableViewCell";
WSTableViewCell *cell = (WSTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[WSTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
WSObject *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell.textLabel setText:item.title];
return cell;
}
Here is the initWithStyle:reuseIdentifier: in my custom UITableViewCell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.textLabel setFont:[UIFont boldSystemFontOfSize:12]];
[self.textLabel setTextColor:[UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0]];
[self.textLabel setHighlightedTextColor:[UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0]];
[self.textLabel setNumberOfLines:2];
}
return self;
}
I’ve just noticed, that this initWithStyle:reuseIdentifier: is called for every cell in the table view, and that the dequeueReusableCellWithIdentifier: in the UITableViewController always returns nil. As far as I understand this mechanism with reusable cells, this kind of cell should only be initialized once when I use the same cell identifier. What am I missing here?
The cells are only reused when they are scrolled off-screen. Let’s say one screen shows 11 cells, you will allocate 11 cells which are reused as you scroll.