自定义 UITableViewCell 选择样式? ?

当我点击我的 UITableViewCell,背景部分(我的背景图片没有覆盖的区域)变成蓝色,当我点击单元格。此外,所有的 UILabel的细胞变成白色时,它被点击,这是我想要的。

然而,我不想要的是蓝色背景,当我点击它,但如果我做 selectionstylenone,然后我失去了突出的颜色为 UILabel的细胞。

那么有没有办法在单击单元格时去掉蓝色背景,而保留 UILabel的突出显示颜色呢?

101433 次浏览

You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and override the default implementation of setHighlighted:animated with your own implementation that sets the label colors to however you want depending on the highlighted state.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}

You can use the following delegate methods after you set the selection style to none:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

Implement your code here, like this

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell.lbls setTextColor:[UIColor whiteColor]];
return indexPath;
}

In cellForRowAtIndexPath use this code:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


[cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels

Hope this will work for you.

Enjoy Coding :)

To get this work you have to set the selection style to UITableViewCellSelectionStyleNone and then you should override the method setSelected:animated: to get the result you want. It does the same thing the automated selection mechanism of iOS does when you see the blue (or gray) selection.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}

You can also customize this in another way, e.g. by changing the UITableViewCell background, etc.

If working before iOS7, make your cell selection style none

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Else, leave it by UITableViewCellSelectionStyleDefault

Then:

UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView =  selectedView;

This code will work properly

In your custom cell, override the default implementation of awakeFromNib & setSelected:

- (void)awakeFromNib {
// Initialization code
UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = [UIImage imageNamed:@"cell_selected_img"];
self.selectedBackgroundView = imageView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];


// Configure the view for the selected state
if (selected) {
self.lblCustomText.textColor = [UIColor whiteColor];
} else {
self.lblCustomText.textColor = [UIColor blackColor];
}
}

Also make sure that the selection style is NOT set to None.

The only way I could get it working was by:

- (void)awakeFromNib {
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(55.0/255.0) green:(163.0/255.0) blue:(237.0/255.0) alpha:1.0];
bgColorView.layer.masksToBounds = YES;
self.selectedBackgroundView = bgColorView;
}

Overriding the following functions in your subclass of UITableViewCell.

override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }

In order to match the standard selection style behavior, you'll want to override both setHighlighted:animated: and setSelected:animated:. You'll probably want to move that code into a shared method in order to avoid duplicate code.

override func setHighlighted(highlighted: Bool, animated: Bool) {


setAsSelectedOrHighlighted(highlighted, animated: animated)
super.setHighlighted(highlighted, animated: animated)
}


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


setAsSelectedOrHighlighted(selected, animated: animated)
super.setSelected(selected, animated: animated)
}


func setAsSelectedOrHighlighted(selectedOrHighlighted: Bool, animated: Bool) {


let action = {
// Set animatable properties
}


if animated {
UIView.animateWithDuration(1.0, delay: 0, options: .CurveEaseInOut, animations: action, completion: nil)
}
else {
action()
}
}

Also you can use contentView.alpha. Here is the example.

First, set selection style for your cell:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Next, in custom cell class override this method with animation example:

  - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];


if (highlighted) {
[UIView animateWithDuration:0.15f animations:^{
self.contentView.alpha = 0.5f;
}];
} else {
[UIView animateWithDuration:0.35f animations:^{
self.contentView.alpha = 1.f;
}];
}
}