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;
}