I am having quite a bit of trouble trying to implement a checkbox as part of a todo list. I have a UITableView, and each UITableViewCell should have its own checkbox, on the lefthand side.
I would appreciate it if someone could show me how to accomplish this. I have looked extensively at the questions on this site and still can't figure out how to implement this.
The issues arise when scrolling the tableview. If I have 20 todo items, for example, when I scroll, the checkboxes that are to be selected, get deselected, and the ones that are to be deselected, get selected, in a seemingly random fashion. It must be something with the scrolling.
What exactly is being cached and happening when the system reuses a cell? Am I grabbing an empty cell with just references to empty subviews inside it?
If tablecell #1 has a view with a tag of 10, will tablecell #2 be able to have a view with a tag of 10? Or are tags not to be repeated throughout the entire tableView? (I'm not even sure that this is the problem)
Thank you!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (selectedSegment) {
case Todos:{
static NSString *ToDoCellIdentifier = @"TodoTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifierToDoCellIdentifier];
UITextView *taskTextView = nil;
UIButton *checkBox = nil;
UIImageView *highPriorityIcon = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:signoutTableViewCellIdentifier] autorelease];
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(1, 1, 25, 25)];
checkBox.tag = indexPath.row;
UIImage *normalImage = [UIImage imageNamed: @"checkbox_unchecked@2x.png"];
UIImage *selectedImage = [UIImage imageNamed: @"checkbox_checked@2x.png"];
[checkBox setImage:normalImage forState:UIControlStateNormal];
[checkBox setImage:selectedImage forState:UIControlStateSelected];
[checkBox addTarget:self action:@selector(historyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:checkBox];
[checkBox release];
//cell.contentView.backgroundColor = [UIColor lightGrayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(@"creating a new cell");
}
if (!checkBox) {
checkBox = (UIButton*)[cell viewWithTag:indexPath.row];
}
}
if ([[self.tasksCheckedStates objectAtIndex:indexPath.row]boolValue]) {
checkBox.selected = YES;
}
else {
checkBox.selected = NO;
}
return cell;
break;
}
You need to do the following:
EDIT
You posted your code as I was writing my answer! I think your problem is the tag you are assigning to the checkbox. You shouldn't be using the index path row for this, as you will have no reference to this by the time you have scrolled down to, say, row 20. Give the checkbox the same tag for all cells and you should be ok. You use the tags to distinguish between views within a superview, so there is no problem with the checkbox having the same tag in all cells.
A further point on your other question: when a cell is dequeued from the table, you get the original object back, with all its views and data as used for the previous time. So if you did nothing except dequeue a cell, create it if missing, then returned it, your first screenful of data would just keep repeating. The normal course of operations, which you are already following except for the tagging issue, is