Changing background color of selected cell?

后端 未结 30 2683
太阳男子
太阳男子 2020-11-29 00:22

Does anyone know how to change the background color of a cell using UITableViewCell, for each selected cell? I created this UITableViewCell inside the code for TableView.

相关标签:
30条回答
  • 2020-11-29 00:41

    SWIFT 5.X
    It also works when accessoryType changed for cell

    extension UITableViewCell{
        var selectedBackgroundColor: UIColor?{
            set{
                let customColorView = UIView()
                customColorView.backgroundColor = newValue
                selectedBackgroundView = customColorView
            }
            get{
                return selectedBackgroundView?.backgroundColor
            }
        }
    }
    

    And in UIViewController use like below...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell
        cell.selectedBackgroundColor = UIColor.lightGray
        return cell
      }
    
    0 讨论(0)
  • 2020-11-29 00:41

    I had a recent issue with an update to Swift 5 where the table view would flash select and then deselect the selected cell. I tried several of the solutions here and none worked. The solution is setting clearsSelectionOnViewWillAppear to false.

    I had previously used the UIView and selectedBackgroundColor property, so I kept with that approach.

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "popoverCell", for: indexPath) as! PopoverCell
    
      let backgroundView = UIView()
      backgroundView.backgroundColor = Color.Blue
      cell.selectedBackgroundView = backgroundView
    }
    

    Below are the changes I needed for Swift 5. The property clearsSelectionOnViewWillAppear was the reason my cells were deselecting. The following select was necessary on first load.

    override func viewDidLoad() {
      super.viewDidLoad()
    
      clearsSelectionOnViewWillAppear = false
      popoverTableView.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
    }
    
    0 讨论(0)
  • 2020-11-29 00:42

    SWIFT 4, XCODE 9, IOS 11

    After some testing this WILL remove the background color when deselected or cell is tapped a second time when table view Selection is set to "Multiple Selection". Also works when table view Style is set to "Grouped".

    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if let cell = tableView.cellForRow(at: indexPath) {
                cell.contentView.backgroundColor = UIColor.darkGray
            }
        }
    }
    

    Note: In order for this to work as you see below, your cell's Selection property can be set to anything BUT None.

    How it looks with different options

    Style: Plain, Selection: Single Selection

    Style: Plain, Selection: Multiple Selection

    Style: Grouped, Selection: Multiple Selection

    Bonus - Animation

    For a smoother color transition, try some animation:

    extension ViewController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if let cell = tableView.cellForRow(at: indexPath) {
                UIView.animate(withDuration: 0.3, animations: {
                    cell.contentView.backgroundColor = UIColor.darkGray
                })
            }
        }
    }
    

    Bonus - Text and Image Changing

    You may notice the icon and text color also changing when cell is selected. This happens automatically when you set the UIImage and UILabel Highlighted properties

    UIImage

    1. Supply two colored images:

    1. Set the Highlighted image property:

    UILabel

    Just supply a color for the Highlighted property:

    0 讨论(0)
  • 2020-11-29 00:42
    var last_selected:IndexPath!
    

    define last_selected:IndexPath inside the class

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! Cell
        cell.contentView.backgroundColor = UIColor.lightGray
        cell.txt.textColor = UIColor.red
    
        if(last_selected != nil){
            //deselect
            let deselect_cell = tableView.cellForRow(at: last_selected) as! Cell
            deselect_cell.contentView.backgroundColor = UIColor.white
            deselect_cell.txt.textColor = UIColor.black
        }
    
        last_selected = indexPath
    }
    
    0 讨论(0)
  • 2020-11-29 00:43
    // animate between regular and selected state
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
        [super setSelected:selected animated:animated];
    
        if (selected) {
            self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f];
        }
        else {
            self.backgroundColor = [UIColor clearColor];
        }
    }
    
    0 讨论(0)
  • 2020-11-29 00:45
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
         UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
         cell.contentView.backgroundColor = [UIColor yellowColor];
    
    }
    
    0 讨论(0)
提交回复
热议问题