UITableViewCell Accessory Type Checked on Tap & Set other unchecked

后端 未结 9 2311
小蘑菇
小蘑菇 2020-12-03 03:02

I am confused a little bit about settings table view cell accessories.

I have fixed two sections in my table

  • Home
  • Office

What I

9条回答
  •  余生分开走
    2020-12-03 03:20

    BlackBerry's answer in Swift 3. UITableView with standard cells, selecting 0 or 1 cells.

    private var previousSelection = NSIndexPath()
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let cell = tableView.cellForRow(at: indexPath)!
        if cell.accessoryType == .none {
            cell.accessoryType = .checkmark
            selection = indexPath
    
            if previousSelection == IndexPath() {
                previousSelection = indexPath
            } else if previousSelection != indexPath {
                let previousCell = tableView.cellForRow(at: previousSelection)
                previousCell?.accessoryType = .none
                previousSelection = indexPath
            }
        } else if cell.accessoryType == .checkmark {
            cell.accessoryType = .none
            selection = IndexPath()
        }
    
    }
    

提交回复
热议问题