UITableViewCellAccessory的问题(​​选中标记/无切换)

时间:2011-03-02 18:10:18

标签: iphone uitableview

edit
那么现在我不觉得自己像个白痴。到目前为止,我还没有关注细胞是否有复选标记。不知怎的,我翻了UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryNone,所以当我希望它打开时它会关闭,当我想要它时它会打开。正确阅读代码确实有助于调试...
/edit

以下代码我遇到了很多麻烦。
来自tableView:didSelectRowAtIndexPath:

if (i.need == 0) { // item not needed - hide (#) and turn on checkmark
    i.need = 1;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", i.name];
    cell.accessoryType = UITableViewCellAccessoryNone;
} else if (i.need < 0) { // item not needed - hide (#) and turn on checkmark
    i.need = -i.need;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", i.name];
    cell.accessoryType = UITableViewCellAccessoryNone;
} else { // item not needed - show (#) and turn off checkmark
    i.need = -i.need;
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d)", i.name, -i.need];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

以下是您犯这个错误的原因。

Tap row with checkmark: (#) hidden, checkmark removed
Tap row without checkmark: (#) shown, checkmark not set until next tap

1 个答案:

答案 0 :(得分:0)

我不确定我是怎么犯错误的,没有连接评论和配件,但你去了。这就是应该的样子:

if (i.need == 0) { // item not needed - hide (#) and turn on checkmark
    i.need = 1;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", i.name];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (i.need < 0) { // item not needed - hide (#) and turn on checkmark
    i.need = -i.need;
    cell.textLabel.text = [NSString stringWithFormat:@"%@", i.name];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else { // item not needed - show (#) and turn off checkmark
    i.need = -i.need;
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d)",
                                                            i.name, -i.need];
    cell.accessoryType = UITableViewCellAccessoryNone;
}