Changing background color of selected cell?

2019-01-07 07:23发布

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.

24条回答
姐就是有狂的资本
2楼-- · 2019-01-07 07:57

Create a custom UITableViewCell. Inside you custom class override the "setSelected" function and change the contentView background color. You can also override you "setHighlighted" function.

In Swift:

class myTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
        // Add your color here
        self.contentView.backgroundColor = UIColor.whiteColor()
    }

    override func setHighlighted(highlighted: Bool, animated: Bool) {
        // Add your color here
        self.contentView.backgroundColor = UIColor.whiteColor()
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-07 07:59

Changing the property selectedBackgroundView is correct and the simplest way. I use the following code to change the selection color:

// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
查看更多
走好不送
4楼-- · 2019-01-07 08:01

Check out AdvancedTableViewCells in Apple's sample code.

You'll want to use the composite cell pattern.

查看更多
ら.Afraid
5楼-- · 2019-01-07 08:01

Works for me

UIView *customColorView = [[UIView alloc] init];
    customColorView.backgroundColor = [UIColor colorWithRed:180/255.0 
                                                      green:138/255.0 
                                                       blue:171/255.0 
                                                      alpha:0.5];
    cell.selectedBackgroundView =  customColorView;
查看更多
够拽才男人
6楼-- · 2019-01-07 08:01

I have tried each one among above answers, but none of them best fits for me,

then i have looked into one of the native provided method, and it is working fine.

first, make cellSelectionStyle to None and then go for this solution.

func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath?
{   
    let cell = tableView.cellForRow(at: indexPath);

   //cell which is getting deselected, make whatever changes that are required to make it back normal        

    cell.backgroundColor = kNormalColor;

    return indexPath;
}

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?
{
    let cell = tableView.cellForRow(at: indexPath);

   //cell which is getting selected, make whatever changes that are required to make it selected        

    cell.backgroundColor = kSelectedColor;

    return indexPath;
}

advantage of this methods over other all is :

  1. It works for multiple cell selection
  2. You can change any element, whichever you want, not only background color of given cell when it get selected as well as deselected.
查看更多
狗以群分
7楼-- · 2019-01-07 08:04
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    if selected {
        self.contentView.backgroundColor = .black
    } else {
        self.contentView.backgroundColor = .white
    }
}
查看更多
登录 后发表回答