UITableViewCell Selected Background Color on Multiple Selection

后端 未结 14 1046
夕颜
夕颜 2020-12-08 04:26
// Doesn\'t work
cell.selectionStyle = .Blue
//Works when the selection is not multiple, if it\'s multiple with each selection the previous one disappear...
let cell         


        
14条回答
  •  被撕碎了的回忆
    2020-12-08 04:47

    Swift 4

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    {
        let selectedCell = tableView.cellForRow(at: indexPath)! as! LeftMenuCell
        selectedCell.contentView.backgroundColor = UIColor.blue
    }
    

    If you want to unselect the previous cell, also you can use the different logic for this

    var tempcheck = 9999
    var lastrow = IndexPath()
    var lastcolor = UIColor()
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if tempcheck == 9999
        {
            tempcheck = 0
            let selectedCell = tableView.cellForRow(at: indexPath)! as! HealthTipsCell
            lastcolor = selectedCell.contentView.backgroundColor!
            selectedCell.contentView.backgroundColor = UIColor.blue
            lastrow = indexPath
        }
        else
        {
            let selectedCelllasttime = tableView.cellForRow(at: lastrow)! as! HealthTipsCell
            selectedCelllasttime.contentView.backgroundColor = lastcolor
            let selectedCell = tableView.cellForRow(at: indexPath)! as! HealthTipsCell
            lastcolor = selectedCell.contentView.backgroundColor!
            selectedCell.contentView.backgroundColor = UIColor.blue
            lastrow = indexPath
        }
    }
    

提交回复
热议问题