我在 iOS 中遇到了一个关于表格 View 的奇怪问题。在表格中,当我点击一行时,我想要显示一个复选标记,它与下面的代码一起工作得很好:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
}
现在,我的表格有 11 行。当我单击第一行时,复选标记出现在第一行(这很好),但它也出现在第 10 行。当我点击第 2 行时,第 2(这很好)和第 11 行出现。
有人知道这个问题吗?
最佳答案
这样做...
self.tableView.allowsMultipleSelection = YES;
然后当你需要获取所有选中的行时...
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
此数组将包含所有带有复选标记的行。
无需自己在代码中做任何事情。
关于ios - 选择一行是检查另一行的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24262292/