迅捷 > 3.0
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .none }}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .checkmark }}
我通过使用两个Swift函数解决了:didSelectRowAtIndexPath和didDeselectRowAtIndexPath。
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .None }}override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .Checkmark }}
为了使其正常工作,请在您的cellForRowAtIndexPath函数中添加一行代码,以在屏幕上绘制表格视图时选择一行,否则,第一次选择另一行时,didDeselectRowAtIndexPath将不会被调用。像这样:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath) if (some condition to initially checkmark a row) cell.accessoryType = .Checkmark tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom) } else { cell.accessoryType = .None } return cell}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)