Ios UITableView单元格上的复选标记
在名为_selectAttributes的UITableView中,当我点击每个单元格时,我希望放置和删除复选标记。 代码如下:Ios UITableView单元格上的复选标记,ios,uitableview,Ios,Uitableview,在名为_selectAttributes的UITableView中,当我点击每个单元格时,我希望放置和删除复选标记。 代码如下: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [_selectAttributes cellForRowAtIndexPath:indexPath]; if (c
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [_selectAttributes cellForRowAtIndexPath:indexPath];
if (cell.accessoryType != UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
一切似乎都很好,但当我在表格上下滚动时,复选标记出现在其他单元格上,而在前一个单元格上消失
我怎样才能解决这个问题
提前谢谢。我将创建一个选定索引路径的NSArray。在
tableView:didselectrowatinexpath:
上,将索引路径添加到该数组中,然后在tableView:cellforrowatinexpath:
中检查索引路径是否在数组中,并相应地设置UITableViewCell的附件类型 声明名为selectedIndexPath
的NSIndexPath
属性
然后让您的委托方法cellforrowatinexpath
和didselectrowatinexpath
如下所示:
- (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;
}
更新:
我并没有注意到你们想要多单元选择的解决方案。我的回答显然只解决了单个单元格选择的问题,但我相信这是一个好的开始。可能重复的。顺便说一句,您看到的行为是UITableView如何重用单元格的结果。表视图这样做是出于性能原因,但它会在许多方面造成麻烦。如果您还没有熟悉单元重用的概念,我建议您熟悉单元重用。