选择和取消选择UITableViewCells - Swift

Joh*_*ley 2 uitableview didselectrowatindexpath ios swift

目前我可以点击一个单元格并使用它来选择它didSelectRowAtIndexPath.但是,点击时我无法取消选择它.我希望切换这两个功能.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    selectedCell.backgroundColor = UIColor.purpleColor()

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}
Run Code Online (Sandbox Code Playgroud)
  • 我有一个TableView在我的View Controller.
  • dataSourcedelegate已经成立.
  • UIBuilder为表格启用了多个选择.

Joh*_*ley 8

我本来可以发誓我曾经尝试过这个,但之后就没用了.

我决定使用两个函数..

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    selectedCell.backgroundColor = UIColor.purpleColor()        
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var deselectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    deselectedCell.backgroundColor = UIColor.clearColor()
}
Run Code Online (Sandbox Code Playgroud)