Custom UI TableViewCell selected backgroundcolor swift

前端 未结 11 906
盖世英雄少女心
盖世英雄少女心 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:08

    I have a likeness problem. In your cellForRowAtIndexPath method set:

    cell.selectionStyle = .None
    

    and then set didHighlightRowAtIndexPath...

    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        let cell  = tableView.cellForRow(at: indexPath)
        cell!.contentView.backgroundColor = .green
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        let cell  = tableView.cellForRow(at: indexPath)
        cell!.contentView.backgroundColor = .clear
    }
    
    0 讨论(0)
  • 2020-12-28 17:09
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        updateUI(isSelected: selected)
    }
    
    private func updateUI(isSelected: Bool){
       label.textColor = isSelected ? .red : .green
       //etc..
    }
    
    0 讨论(0)
  • 2020-12-28 17:11

    You've the right method already in there: didSelectRowAtIndexPath. In that method you can call tableView.cellForRowAtIndexPath(indexPath) and get your cell. Than you can set the cell-background to your color:

     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
            println("Row \(indexPath.row) selected")
            let cell:YourCustomCell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
            cell.backgroundColor = UIColor.redColor()
        }
    

    Or, a better way would be to check in your cellForRowAtIndexPath method, if a cell is selected:

    if(cell.selected){
      cell.backgroundColor = UIColor.redColor()
    }else{
      cell.backgroundColor = UIColor.clearColor()
    }
    
    0 讨论(0)
  • 2020-12-28 17:16

    The correct (more native & natural) way to do it:

    override func awakeFromNib() {
        super.awakeFromNib()
        selectedBackgroundView = UIView()
        selectedBackgroundView?.backgroundColor = .blue
    }
    

    Why other approaches are wrong:

    1. Highlight solution: highlight is not select. Highlight doesn't animate as Selection does. It doesn't feel natural as a normal Selection would do
    2. No need to change the color of selectedBackgroundView in setSelected method, because iOS handles the animation for us.
    3. Setting backgroundColor also doesn't behave the same as iOS does it
    0 讨论(0)
  • 2020-12-28 17:16

    None of above answers worked, so I try mine and it worked: Here it is:-

    1. In your cell create function like this.
    func reloadcell() {
        if isSelected {
            conView.backgroundColor = .yellow
        } else if isHighlighted {
            conView.backgroundColor = .yellow
        } else {
            conView.backgroundColor = .clear
        }
    }
    

    and call that function in layoutsubviews and in your didselect method

    0 讨论(0)
  • 2020-12-28 17:18

    To keep your code clean you should think about moving screen design related code for your cells from UITableViewController into a UITableViewCell class.

    Your UITableViewController` needs only to set the selected state of the cell as follows:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        guard let cell = tableView.cellForRow(at: indexPath) else { return }
        cell.setSelected(true, animated: true)
    }
    

    Your desired customisation can be implemented in a derrived UITableViewCell class by overriding var isSelected. With this solution you could have even different select colors for each cell.

    class MyTableViewCell: UITableViewCell
    {
        @IBOutlet weak var label:UILabel!
    
        override var isSelected: Bool
        {
            didSet{
                if (isSelected)
                {
                    self.backgroundColor = UIColor.red
                    if let label = label
                    {
                        label.textColor = UIColor.white
                    }
                }
                else
                {
                    self.backgroundColor = UIColor.white
                    if let label = label
                    {
                        label.textColor = UIColor.black
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题