Swift:保存并显示选定的indexPath在静态表视图
问题描述:
我有一个小问题,我的静态tableview。我有一个弹出窗口显示为我的网格选项。我要救我的静态TableView中的状态(indexPath),但似乎它不工作,下面是我的代码片段:Swift:保存并显示选定的indexPath在静态表视图
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var currSelected: IndexPath?
let section = indexPath.section
let numberOfRows = tableView.numberOfRows(inSection: section)
for row in 0..<numberOfRows {
if let cell = tableView.cellForRow(at: NSIndexPath(row: row, section: section) as IndexPath) {
cell.accessoryType = row == indexPath.row ? .checkmark : .none
tableView.deselectRow(at: indexPath, animated: false)
currSelected = indexPath
if section == 2 {
tableView.deselectRow(at: indexPath, animated: false)
cell.accessoryType = .none
}
else {
delegate?.option(lastSelected: currSelected!)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reload"), object: nil)
}
}
}
}
我试图用delegate
扔lastSelected
(最后indexPath)以前控制器并将其发送回Pop Over Controller,然后我就得到了它。但我不知道使用lastSelected显示复选标记。而prepare for cell at
函数需要一个重用单元的标识符,但我不使用它。
答
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath == lastSelected {
cell.accessoryType = .Checkmark
}
}
其唯一的保存一个选中标记。我有2个部分,不能chekced,所以我需要2复选标记,如果lastSelected有两个复选标记 –
然后,您需要存储IndexPaths数组而不是IndexPath变量,并在'willDisplayCell',通过该数组循环 – Malik
其工作与分离索引路径。接受为有用的sugest,我会更新我的最终代码。提前致谢 –