I have a tableview with a lot of entries divided into multiple sections and inside that I have multiple rows.I have a button modelled as a checkbox in each rows. The checkboxes(buttons) can be activated/deactivated when pressed. I am loading an image on the button(checkbox) based on the status, whether it is checked or not. Everything is going fine, until I try to drag my UITableview downwards and then go up, only to find some of my checked checkboxes automatically unchecked. I kind of figured that, this was caused due to the use of
dequeuereusablecellwithidentifier
I want to avoid this strange behavior of my tableview. Need help in this case.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIButton *checkBox = [[UIButton alloc] init];
checkBox.tag = contact.contactID;
[cell.contentView addSubview:checkBox];
[checkBox setFrame:CGRectMake(6,14,20,20)];
[checkBox release];
}
UIButton *checkBox = (UIButton *)[cell.contentView viewWithTag:contact.contactID];
if(isActivDeactivButton)
{
[checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal];
}
else{
[checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];
}
return cell;
}
I replaced
with
This solved the problem as my contactID values were changing during scroll.