所以我试图在这里建立某种待办事项列表——我有一个表格视图,在每个单元格上都有一个代表复选框的缩略图。在触摸时,我希望它改变图像 - 从满到空,反之亦然。我做错了什么?这是一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"taskCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 32, 32)];
[button addTarget:self action:@selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"checkbox_empty"] forState:UIControlStateNormal];
[button setTag:101];
[cell addSubview:button];
[cell setIndentationWidth:36];
[cell setIndentationLevel:1];
cell.textLabel.text = [[self.appDel.allTasks objectAtIndex:indexPath.row] taskName];
return cell;
}
- (void) checkmarkPressed:(UIButton *)sender{
if(sender.tag == 101){
UITableViewCell *cell = ((UITableViewCell*)[sender superview]);
NSLog(@"Cell %i", [self.tableView indexPathForCell:cell].row);
UIImage *empty = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"checkbox_empty" ofType:@"png"]];
UIImage *full = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"checkbox_full" ofType:@"png"]];
if ([sender imageView].image == [UIImage imageNamed:@"checkbox_empty"]) {
[sender setImage:full forState:UIControlStateNormal];
[self.tableView reloadData];
}
if ([sender imageView].image == full) {
[sender setImage:empty forState:UIControlStateNormal];
[self.tableView reloadData];
}
}
}