在我的cellForRowAtIndexPath中,我预先选择一行:
NSArray *tempArray = [[communityPrefs objectForKey:@"Community"] componentsSeparatedByString:@","];
NSMutableArray *tempArrayMutable = [[NSMutableArray alloc] initWithArray:tempArray];
if ([tempArrayMutable containsObject:cell.textLabel.text])
{
[selectedAreaTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
这部分效果很好。但是,所选项目位于页面底部,用户将看不到它,但当它们向下滚动到该单元格时,它将被选中。
现在我正在尝试编写一段代码,将取消选择所有选定的单元格,如下所示:
for(NSIndexPath *index in selectedAreaTable.indexPathsForSelectedRows)
{
if(index.row != 0)
{
[selectedAreaTable deselectRowAtIndexPath:index animated:NO];
}
}
但是在运行此代码后,仍然会选择底部的单元格。所以我的问题是,为什么这个细胞没有被取消选择?是因为它滚到它之前不存在吗?我该如何解决这个问题?
答案 0 :(得分:0)
是不是因为它滚到它之前不存在?
是。我相信你应该改变选择"仅将单元格对象用于可见行的状态。所有其他行应检索"选择" cellForRowAtIndexPath
方法
答案 1 :(得分:0)
user979331,
你不必在单独的方法中删除选择,而你也可以在cellForRowAtIndexPath中处理它。
您可以将数组NSMutableArray * tempArrayMutable声明为属性,
当您想要取消选择所有单元格时,我们假设一个名为clear all selection
的方法 -(void)clearAllSelection {
[self.tempArrayMutable removeAllObjects];
self.tableView reloadData];
}
最后在cellForRowAtIndexPath中更改为
if ([self.tempArrayMutable containsObject:cell.textLabel.text])
{
[selectedAreaTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
else{
[selectedAreaTable deselectRowAtIndexPath:indexPath animated:NO];
}