How to deselect a selected UITableView cell?

前端 未结 24 3160
一整个雨季
一整个雨季 2020-12-07 10:10

I am working on a project on which I have to preselect a particular cell.

I can preselect a cell using -willDisplayCell, but I can\'t deselect it when t

24条回答
  •  不思量自难忘°
    2020-12-07 10:18

    What I've found is that on top of setting the cell as selected, you have to let the table view know to select the row at the given index path.

    // Swift 3+  
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if appDelegate.indexPathDelegate.row == indexPath.row {
            self.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
            cell.setSelected(true, animated: true)
        }
    }
    

提交回复
热议问题