UITableViewCell Selected Background Color on Multiple Selection

后端 未结 14 967
夕颜
夕颜 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:42

    SWIFT 3/4

    Solution for CustomCell.selectionStyle = .none if you set some else style you saw "mixed" background color with gray or blue.

    And don't forget! func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) didn't call when CustomCell.selectionStyle = .none.

    extension MenuView: UITableViewDelegate {
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cellType = menuItems[indexPath.row]
            let selectedCell = tableView.cellForRow(at: indexPath)!
                selectedCell.contentView.backgroundColor = cellType == .none ? .clear : AppDelegate.statusbar?.backgroundColor?.withAlphaComponent(0.15)
    
            menuItemDidTap?(menuItems[indexPath.row])
    
            UIView.animate(withDuration: 0.15) {
                selectedCell.contentView.backgroundColor = .clear
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 04:43

    By adding a custom view with the background color of your own you can have a custom selection style in table view.

    let customBGColorView = UIView()
    customBGColorView.backgroundColor = UIColor(hexString: "#FFF900")
    cellObj.selectedBackgroundView = customBGColorView
    

    Add this 3 line code in cellForRowAt method of TableView. I have used an extension in UIColor to add color with hexcode. Put this extension code at the end of any Class(Outside the class's body).

    extension UIColor {    
    convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.characters.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
      }
    }
    
    0 讨论(0)
  • 2020-12-08 04:45

    Swift 5 - This works for me:

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
            selectedCell.contentView.backgroundColor = .red
        }
    
    
    
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    
            let cellToDeSelect:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
            cellToDeSelect.contentView.backgroundColor = .clear
        }
    
    0 讨论(0)
  • 2020-12-08 04:46

    You can also set cell's selectionStyle to.none in interface builder. The same solution as @AhmedLotfy provided, only from IB.

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 2020-12-08 04:48

    The problem with Kersnowski's approach is that when the cell is redrawn the changes made when it's selected/deselected will be gone. So I would move the changes into the cell itself, which means subclassing is required here. For example:

    class ICComplaintCategoryCell: UITableViewCell {
        @IBOutlet var label_title: UILabel!
        @IBOutlet var label_checkmark: UILabel!
    
        override func layoutSubviews() {
            super.layoutSubviews()
            reload()
        }
        func reload() {
            if isSelected {
                contentView.backgroundColor = UIColor.red
            }
            else if isHighlighted{
                contentView.backgroundColor = UIColor.red
            }
            else {
                contentView.backgroundColor = UIColor.white
            }
        }
    }
    

    And in your table view delegate just call reload:

    if let cell = self.table.cellForRowAtIndexPath(path) as? ICComplaintCategoryCell {
        cell.reload()
    }
    

    Updated for Swift 3+, thanks @Bogy

    0 讨论(0)
提交回复
热议问题