I’m trying to insert some UILabels into the first row of a UITableView, but I’m getting unexpected results. For sake of brevity, here’s a summary:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* identifier = @"cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [[UITableViewCell alloc[ .....
cell.textLabel.text = @"";
if (indexPath.row == 0) {
cell.backgroundColor = ....
UILabel* x = .....
[x setTag:1];
[cell insertSubview:x];
}
else {
cell.textLabel.text = ....
for (UIView* view in cell.subviews) {
if (view.tag == 1)
[view removeFromSuperview];
}
}
return cell
}
That’s the gist anyway. The problem is that while the background color is being set correctly, anytime I scroll the table view, the custom UILabel I insert disappears. Maybe this stems from a misunderstanding of dequeueReusableCellWithIdentifier but regardless, I would think that the label would be reinserted.
The best way to do that is to use different cell identifiers, and do the full initializing of the cell when it’s created, instead of creating, adding and removing a lot of views each time a cell is requested.