在Swift 4 UITableView中的选定行上添加/删除多个选中标记 [英] Add/Remove Multiple Checkmarks on Selected Rows in Swift 4 UITableView

查看:130
本文介绍了在Swift 4 UITableView中的选定行上添加/删除多个选中标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

询问,因为所有可用的解决方案都适用于Swift3。
另外,我正在处理一个问题,当我突出显示+选择一行后上下滚动时,出现具有相同indexPath的新行(滚动后) )也会被选中(但不会突出显示)。

Asking because all solutions available are for Swift 3. Also, I am dealing with a problem that when I scroll up and down after highlighting + selecting a row, new rows that appear with the same indexPath (after scrolling) will also be checked (but not highlighted).

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {    
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark
    }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .none
    }
}


推荐答案

代替 didSelectRowAt accessoryType >和 didDeselectRowAt 方法,则应覆盖并在 setSelected(_:animated :)

Instead of change accessoryType on didSelectRowAt and didDeselectRowAt methods, you should override and do it on setSelected(_:animated:) from your cell class.

class YourCellClass: UITableViewCell {

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    if selected {
      accessoryType = .checkmark
    } else {
      accessoryType = .none
    }
  }

}

这篇关于在Swift 4 UITableView中的选定行上添加/删除多个选中标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆