UITableViewCell blank when reused

I’m using UITableView with various kinds of cells defined in the storyboard. The styles are all “Subtitle.” For one cell type, the Identifier is “label,” it’s used in my table in two locations, and is fetched thusly:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label" forIndexPath:indexPath];

The code to populate the text labels is:

// cell {0,0} {section,row}
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"Subtitle 1";
NSLog(@"%@",cell); // <UITableViewCell: 0x13ee183c0; frame = (0 26; 320 52); text = ''; ...

And

// cell {6,0}
cell.textLabel.text = @"Version";
cell.detailTextLabel.text = @""; // using @" " instead cures the problem
NSLog(@"%@",cell); // <UITableViewCell: 0x13ee183c0; frame = (0 1022; 320 52); text = 'Version'; ...

At launch, cell {0,0} shows the proper text. When I scroll down, cell {6,0} shows the proper text. But, when I scroll back to the top, cell {0,0} is blank (the reused cell.text = 'Version'). If I scroll cell {0,0} out of view, then scroll back to the top, cell {0,0} is okay (the reused cell.text = '').

In cell {6,0}, if I use @" " instead of @"" for detailTextLabel, then the problem goes away, but the spacing in the cell is incorrect (“Version” is no longer vertically centered).

It’s curious that cell.text (a deprecated property) is not empty in the second case, but explicitly setting cell.text back to nil or @"" in {0.0} makes no difference.

Why is the first cell blank when reused in section 6?


Source: ios

Leave a Reply