gpt4 book ai didi

ios - UITableView 单元格上的复选标记

转载 作者:行者123 更新时间:2023-11-28 22:14:21 26 4
gpt4 key购买 nike

在我名为 _selectAttributes 的 UITableView 中,我想在点击每个单元格时添加和删除复选标记。这是代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [_selectAttributes cellForRowAtIndexPath:indexPath];
if (cell.accessoryType != UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}

一切似乎都运行良好,但是当我上下滚动表格时,复选标记出现在其他单元格上并在之前的单元格上消失。

我该如何解决?

提前谢谢你。

最佳答案

声明一个名为 selectedIndexPathNSIndexPath 属性。

然后让您的委托(delegate)方法 cellForRowAtIndexPathdidSelectRowAtIndexPath 像这样:

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

if ([indexPath isEqual:self.selectedIndexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
cell.accessoryType = UITableViewCellAccessoryNone;

cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectedIndexPath = indexPath;
}

更新:我没有注意到您需要多个单元格选择的解决方案。我的回答显然只解决了单细胞选择的问题,但我相信这是一个好的开始。

关于ios - UITableView 单元格上的复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com