I have an UITableView and when I select a cell I wanna put a checkmark on the right of the cell….which is pretty easy.
To this end I did the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewMieux deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-pressed.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else if (selectedCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryNone;
}
[tableViewMieux reloadData];
}
Everything works great except the fact that when I select the first cell this gets checkmarked and the fifth cell gets checkmarked automatically too.
And so on…if I select the second cell this gets checkmarked …and another cell from the bottom of the tableView gets marked too.
So, each time I select and mark a cell and extra cell gets marked.
Question:Why?What is the right way to do it?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewMieux dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
return cell;
}
}
Simply try this …