0

所以我试图在这里建立某种待办事项列表——我有一个表格视图,在每个单元格上都有一个代表复选框的缩略图。在触摸时,我希望它改变图像 - 从满到空,反之亦然。我做错了什么?这是一些代码:

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

}

4

2 回答 2

0

如果你只是想改变触摸图像,你可以试试这个代码

- (void) checkmarkPressed:(UIButton *)tempButton{
    if (tempButton.tag == 101) {
        tempButton.tag = 104;
        [sender setImage:full forState:UIControlStateNormal];
    }
    else if (tempButton.tag == 104) {
        tempButton.tag = 101;
        [sender setImage:empty forState:UIControlStateNormal];
    }
    [self.tableView reloadData];
}
于 2012-07-06T11:27:13.253 回答
0

找到解决方案:

if ([sender imageView].image == [UIImage imageNamed:@"checkbox_empty"]) {
        [sender removeFromSuperview];
        [sender setImage:full forState:UIControlStateNormal];
        [[sender superview] addSubview:sender];
        [self.tableView reloadData];
    }
    else if ([sender imageView].image == [UIImage imageNamed:@"checkbox_full"]) {
        [sender removeFromSuperview];
        [sender setImage:empty forState:UIControlStateNormal];
        [[sender superview] addSubview:sender];
        [self.tableView reloadData];
    }
于 2012-07-06T13:26:38.267 回答