-2

every 12 cells in my tableview have the same address, this results in a problem: when i address to one cell, all cells with that address get called

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"datacell";
DataCell *cell = (DataCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil) {
    cell= [[DataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...

if (indexPath.row==1) {
    cell.backgroundColor= [UIColor redColor];
}
return cell;

for example here, although i set red color to only one cell, every 13-th cell gets red background. So i have 4 cells with red background. I have no idea what is going on :@:@

Dmitrii G.
  • 895
  • 1
  • 7
  • 21

1 Answers1

2

Table view cells are reused when you scroll. For that reason, you have to always set the properties of the cell, e.g.:

if (indexPath.row==1) {
    cell.backgroundColor= [UIColor redColor];
} else {
    cell.backgroundColor= [UIColor clearColor];
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382