Custom UI TableViewCell selected backgroundcolor swift

前端 未结 11 967
盖世英雄少女心
盖世英雄少女心 2020-12-28 16:51

I am trying to change the appearance of a custom selected TableViewCell using Swift.

Do I need to do it via the designer or programmatically?

I tried the fol

11条回答
  •  孤独总比滥情好
    2020-12-28 17:22

    Update for Swift 3

    This answer is based on Cao Yong answer, and it is intended as an update for Swift 3

    For Swift 3, use the following code in your cellForRowAt indexPath method set:

    cell.selectionStyle = .none
    

    Then, set it in didHighlightRowAtIndexPath

    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        let cell  = tableView.cellForRow(at: indexPath)
        cell!.contentView.backgroundColor = .red
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        let cell  = tableView.cellForRow(at: indexPath)
        cell!.contentView.backgroundColor = .clear
    }
    

提交回复
热议问题