UITableViewCell Selected Background Color on Multiple Selection

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

    For Swift 3,4 and 5 you can do this in two ways.

    1) class: UITableViewCell

    override func awakeFromNib() {
        super.awakeFromNib()
        //Costumize cell
    
        selectionStyle = .none
    }
    

    or

    2) tableView cellForRowAt

        cell.selectionStyle = .none
    

    If you want to set selection color for specific cell, check this answer: https://stackoverflow.com/a/56166325/7987502

    0 讨论(0)
  • 2020-12-08 04:49

    Swift 3

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath)
        cell.selectionStyle = .none
        return cell
    }
    

    Swift 2

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath)
         cell.selectionStyle = .None
         return cell
    }
    
    0 讨论(0)
  • Swift 4.2

    For multiple selections you need to set the UITableView property allowsMultipleSelection to true.

    myTableView.allowsMultipleSelection = true

    In case you subclassed the UITableViewCell, you override setSelected(_ selected: Bool, animated: Bool) method in your custom cell class.

     override func setSelected(_ selected: Bool, animated: Bool) {
         super.setSelected(selected, animated: animated)
    
         if selected {
             contentView.backgroundColor = UIColor.green
         } else {
             contentView.backgroundColor = UIColor.blue
         }
     }
    
    0 讨论(0)
  • 2020-12-08 04:54

    You can use standard UITableViewDelegate methods

    - (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        EntityTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell selectMe];
        return indexPath;
    }
    
    - (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        EntityTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell deSelectMe];
        return indexPath;
    }
    

    in my situation this works, cause we need to select cell, change color, and when user taps 2 times on the selected cell further navigation should be performed.

    0 讨论(0)
  • 2020-12-08 04:59

    All the above answers are fine but a bit to complex to my liking. The simplest way to do it is to put some code in the cellForRowAtIndexPath. That way you never have to worry about changing the color when the cell is deselected.

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    
        /* this is where the magic happens, create a UIView and set its
           backgroundColor to what ever color you like then set the cell's
           selectedBackgroundView to your created View */
    
        let backgroundView = UIView()
        backgroundView.backgroundColor = YOUR_COLOR_HERE
        cell.selectedBackgroundView = backgroundView
        return cell
    }
    
    0 讨论(0)
  • 2020-12-08 04:59

    Swift 3

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
         selectedCell.contentView.backgroundColor = UIColor.darkGray
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
         let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
         selectedCell.contentView.backgroundColor = UIColor.clear
    }
    
    0 讨论(0)
提交回复
热议问题