使用UITableViewCellAccessoryCheckMark

4

我试着让这个代码可用,但是无法理解它的意思。

到目前为止,你可以选择一个单元格并且它可以产生一个勾选标记。然后,当你选择另一个单元格时,先前的勾选标记会消失,并且表视图会在你刚刚选择的新单元格上创建一个新的勾选标记。这一切都很完美。

然而,我想让它实现当你选择带有勾选标记的相同单元格时,勾选标记不会消失。但是,它确实会消失!

我尝试了大量的if语句来看看是否能找到解决办法,但是没有找到解决方案。这可能是我需要重排代码中的一些细节问题。

以下是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }

    if([self.checkedIndexPath isEqual:indexPath])
    {
        self.checkedIndexPath = nil;
    }

    else
    {
         cellCheck = [tableView cellForRowAtIndexPath:indexPath];
        cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSLog(@"%@", indexPath);

    }
}
2个回答

12
如果您选择带有复选标记的单元格,则将执行此代码。
if(self.checkedIndexPath)
{
    UITableViewCell* uncheckCell = [tableView
                                     cellForRowAtIndexPath:self.checkedIndexPath];
     uncheckCell.accessoryType = UITableViewCellAccessoryNone;
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这段代码将会移除你在第一次选择时添加的选中标记。

然后这段代码将被执行。

if([self.checkedIndexPath isEqual:indexPath])
{
    self.checkedIndexPath = nil;
}

因此,只有当您再次选择相同的单元格时,复选标记才会重新出现。

我认为更清晰的方式如下所示。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cellCheck = [tableView
                                    cellForRowAtIndexPath:indexPath];
    cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* uncheckCell = [tableView
                                    cellForRowAtIndexPath:indexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
 }

您可以通过[tableView indexPathForSelectedRow]获取self.checkedIndexPath的值;self.checkedIndexPath的设置取决于您代码的逻辑,是可选的。


谢谢您为我澄清这个问题。现在更加简洁易懂了。 - Zack

0

你不应该直接操作单元格,需要将选中状态存储在某个地方,并在 cellForRowAtIndex: 方法中更改 accessoryType。

static BOOL checkedRows[100]; // example data storage

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    checkedRows[indexPath.row] = !checkedRows[indexPath.row];
    [tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"somecell"];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.accessoryType = checkedRows[indexPath.row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return cell;
}

1
实际上,像 OP 显示的更改 accessoryType 是完全可以的。这是一种常见的方法,可以在不需要告诉表视图重新加载两行的情况下取消选中当前行并选中新行。重新加载整个表以更改两行的 accessoryType 是过度和非常低效的。此外,由于他只想选择一个给定的行,因此没有理由使用数组来跟踪所选行。单个值就足够了。 - rmaddy
但由于 TableViewCells 在滚动时会被重用,当您向下滚动一点时,该 UITableViewCell 对象将用于另一行,并将 accessoryType 重置为 cellForRowAtIndex 中的标准值。如果您不重置它,复用时选中标记将出现在其他任意行上,不是吗? - oskob
但是由于您还更新了数据模型,因此当您滚动时,tableView:cellForRowAtIndexPath: 被调用时单元格将被正确更新。最终,您始终会在 cellForRow... 中调整 accessoryType,在 didSelectRow... 中您要么调整 accessoryType,要么重新加载受影响的行。 - rmaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接