当我单击UITableViewCell时,单击单元格时,背景部分(背景图像未覆盖的区域)变为蓝色。另外,UILabel单击该单元格上的所有s都会变成白色,这正是我想要的。
但是,我不希望单击的是蓝色背景,但是如果单击selectionstylenone,则会丢失UILabel单元格中s的突出显示颜色。
那么,有什么方法可以在单击单元格时摆脱蓝色背景,而保持UILabels的突出显示颜色吗?
当我单击UITableViewCell时,单击单元格时,背景部分(背景图像未覆盖的区域)变为蓝色。另外,UILabel单击该单元格上的所有s都会变成白色,这正是我想要的。
但是,我不希望单击的是蓝色背景,但是如果单击selectionstylenone,则会丢失UILabel单元格中s的突出显示颜色。
那么,有什么方法可以在单击单元格时摆脱蓝色背景,而保持UILabels的突出显示颜色吗?
Answers:
您可以按照以下步骤进行操作。将表格单元格的选择样式设置为UITableViewCellSelectionStyleNone。这将删除蓝色背景突出显示。然后,要使文本标签突出显示为所需的方式,而不是使用默认的UITableViewCell类,请创建的子类UITableViewCell并setHighlighted:animated使用自己的实现覆盖默认的实现,该实现将标签颜色设置为所需的颜色,具体取决于突出显示的状态。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}
setHighlighted:animated:,这需要的animated参数。覆盖单参数setHighlighted:方法将不起作用。
                    [super setHighlighted:highlighted animated:animated];吗?
                    setHighlighted和时,此方法存在一个问题setSelected。首先,你选择一个单元格,行的文字颜色改变为白色,然后您滚动该单元格您的屏幕可视部分,然后向后滚动,看到的,文字颜色变黑
                    如果在iOS7之前运行,请设置您的单元格选择样式为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
别这样 UITableViewCellSelectionStyleDefault
然后:
UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView =  selectedView;
此代码将正常工作
将选择样式设置为无后,可以使用以下委托方法:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
像这样在这里实现您的代码
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell.lbls setTextColor:[UIColor whiteColor]];
    return indexPath;
}
为了完成这项工作,您必须将选择样式设置为UITableViewCellSelectionStyleNone,然后应该重写该方法setSelected:animated:以获得所需的结果。当您看到蓝色(或灰色)选择时,它的功能与iOS的自动选择机制相同。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (selected) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}
您还可以通过其他方式自定义此设置,例如,通过更改UITableViewCell背景等。
在cellForRowAtIndexPath中使用以下代码:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels
希望这对您有用。
享受编码:)
在的子类中重写以下函数UITableViewCell。
override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }
为了匹配标准的选择样式行为,您将要同时覆盖setHighlighted:animated:和setSelected:animated:。您可能需要将该代码移到共享方法中,以避免重复代码。
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()
    }
}
在您的自定义单元中,覆盖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];
    }
}
另外,还要确保选择的风格是不设置为无。
您也可以使用contentView.alpha。这是例子。
首先,为您的单元格设置选择样式:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
接下来,在自定义单元格类中用动画示例覆盖此方法:
  - (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;
        }];
    }
  }