我试着让这个代码可用,但是无法理解它的意思。
到目前为止,你可以选择一个单元格并且它可以产生一个勾选标记。然后,当你选择另一个单元格时,先前的勾选标记会消失,并且表视图会在你刚刚选择的新单元格上创建一个新的勾选标记。这一切都很完美。
然而,我想让它实现当你选择带有勾选标记的相同单元格时,勾选标记不会消失。但是,它确实会消失!
我尝试了大量的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);
}
}