想象一个简单的表格 View ,有几个单元格,启用单选,这意味着一旦我点击一个单元格,它就会保持选中状态,即在使用默认控件时为蓝色。当我点击不同的单元格时,前一个单元格被取消选择,当前单元格将被选中。
现在,让它完整(Apple,Apple :-/...)即。为了能够取消选择当前选中的单元格,我们可以使用一段简单的代码
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *touchedCell = [self.tableView cellForRowAtIndexPath:indexPath];
if (touchedCell.isSelected == YES)
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
return nil;
}
else
{
return indexPath;
}
}
不过有一个问题。此方法仅适用于 iOS6 SDK。 您能否为我没有此方法的 iOS5 SDK 提供最简单、最优雅的解决方案?
最佳答案
所有三种方法:
- tableView:willSelectRowAtIndexPath:
- cellForRowAtIndexPath:
- deselectRowAtIndexPath:animated:
根据文档,在 iOS 2.0 及更高版本中可用。
希望这对您有所帮助!
关于ios - 在 iOS5 中单选时取消选择单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18641639/