Log139 Log139 - 12 days ago 9
iOS Question

UITableViewCell style and dequeueReusableCellWithIdentifier

So I register my cell:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// setting up the cell
}


The issue is I can't set the
cell.detailTextLabel.text
property. The cell is never
nil
.

Answer

If called first, table view registerClass will cause dequeueReusableCellWithIdentifier to return non-nil cell if the cell reuse identifier matches.

I believe registerClass is generally used for cells that will be a custom cell derived from UITableViewCell. Your custom cell can overrite initWithStyle and set the style there.

It's not always necessary to create a custom cell.

If you want to set the cell style then don't call registerClass.