0

我有一个如下图所示的表格视图:

当我在缺席部分选择表格行时,我想更改复选框并使用标记复选框

我知道我应该使用这个方法 didSelectRowAtIndexPath: "that when user select the row it should change the picture" 但我不知道如何

你能帮帮我吗

提前致谢!

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

if (indexPath.section != 2) {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text =contentForThisRow;
    return cell;
}
else {
    CheckBoxCompensationViewCell *cell = (CheckBoxCompensationViewCell*)[tableView 
dequeueReusableCellWithIdentifier:@"CheckBoxCompensationViewCell"];

    cell.imageView.image=[UIImage imageNamed:@"emptycheck-box.png"];
    cell.checkBox.image = image;
    if(indexPath.row == 0){
        cell.compensationText.text =@"Red";
    }
    else if (indexPath.row == 1){
        cell.compensationText.text =@"Green";

    }else if(indexPath.row == 2){
        cell.compensationText.text =@"Blue";
    }
    return cell;
}
}



 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
// I don't know what should I have here , probably mark box image but I don't know how
}
4

2 回答 2

2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *previousPath = self.selectedIndexPath;
    self.selectedIndexPath = indexPath;
    [tableView reloadRowsAtIndexPaths:@[previousPath, indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

请注意,这是非常基本的,不会取消选择单元格,您需要检查 cellForRowAtIndexPath 中所选索引路径的状态:

于 2012-08-08T14:26:16.237 回答
0

它是如此容易。当用户选择单元格时,您应该更新数据模型并重新加载表。数据模型存储按钮状态。在 cellForRowAtIndexPath 方法中,根据数据模型状态设置按钮。就这样。

于 2012-08-08T14:28:05.173 回答