【发布时间】:2015-06-16 20:36:00
【问题描述】:
我正在尝试使用类似于“设置”->“常规”->“自动锁定”中的UITableViewController 构建一个选择界面,并且能够使复选标记正常工作,但我似乎无法弄清楚如何再次调用视图时,复选标记会出现在先前选择的单元格上。
到目前为止,这是我的复选标记选择代码:
var lastSelectedIndexPath: NSIndexPath?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedDistance = distances[indexPath.row]
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if indexPath.row != lastSelectedIndexPath?.row {
if let lastSelectedIndexPath = lastSelectedIndexPath {
let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath)
oldCell?.accessoryType = .None
}
let newCell = tableView.cellForRowAtIndexPath(indexPath)
newCell?.accessoryType = .Checkmark
lastSelectedIndexPath = indexPath
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("DataCell") as UITableViewCell
cell.accessoryType = (lastSelectedIndexPath?.row == indexPath.row) ? .Checkmark : .None
let distance = distances[indexPath.row]
cell.textLabel?.text = distance.0
cell.detailTextLabel?.text = "\(distance.1)"
return cell
}
我想我可能必须将 lastSelectedIndexPath 存储在另一个文件中,但我不确定从这里去哪里。非常感谢任何帮助。
【问题讨论】:
标签: swift uitableview