我有这段代码:
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if let selected = self.lastSelected {
tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None
}
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
self.lastSelected = indexPath
我允许我在选择的单元格上打勾,并删除之前的复选标记。我遇到的问题是,如果列表很大并且单元格不在 View 中,则选择新单元格时不会删除复选标记。
我尝试添加 tableview.reloaddata() 但没有任何作用。
想法?
最佳答案
cellForRowAtIndexPath
对于不可见单元格返回 nil
。
要获得类似单选按钮的效果,您需要执行以下操作:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
if indexPath == selectedIndexPath {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
// Note indexPathsForVisibleRows may be nil depending on your app's content.
// zip just joins two arrays together, into a single array of tuples.
zip(tableView.indexPathsForVisibleRows!, tableView.visibleCells).forEach({ indexPath, cell in
configureCell(cell, atIndexPath: indexPath)
})
}
关于swift - 表格 View 单元格附件在 View 之外时不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246184/