我有一个包含两个部分的表视图。部分1(第二部分)中的每个单元都有一个抽头附件和一个取消选择方法,用于在部分1中的另一个单元被抽头时。但是,如果部分0中的任何单元格被点击,也会取消选择当前选定的单元格。
我的代码是:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.section == 1)
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.section == 1)
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None
}
}
我只希望在第1节中的任何其他单元格被选中时取消选择这些单元格,而忽略对任何其他节的取消选择。
有什么想法吗?
为了获得正确的引用,您需要管理globalArray本身中的引用,如下所示:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
.........
if ( globalArray[indexpath.row][SELECTED_STATE] == SELECTED )
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
}
else
{
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None
}
.........
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//remove the pre selected state if needed by iterating the array
globalArray[indexpath.row][SELECTED_STATE] = SELECTED
tableView .reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}