自定义的UITableViewCellselect风格?

当我点击我的UITableViewCell ,背景部分(我的背景图片未覆盖的区域)变成蓝色,当我点击单元格时。 此外,单元格上的所有UILabel在点击时都会变成白色,这正是我想要的。

然而,我不想要的是蓝色背景,当我点击它,但如果我做的selectionstylenone ,然后我失去了单元格中UILabel的高亮颜色。

那么有没有什么办法可以在点击单元格的时候摆脱蓝色的背景,而保持UILabel的高亮颜色?

你可以这样做,如下所示。 将你的表格单元格的select样式设置为UITableViewCellSelectionStyleNone 。 这将删除蓝色背景突出显示。 然后,为了使文本标签突出显示按照您想要的方式工作,而不是使用默认的UITableViewCell类,请创buildUITableViewCell的子类并覆盖setHighlighted:animated的默认实现setHighlighted:animated使用您自己的实现进行setHighlighted:animated设置,然后将标签颜色设置为在突出显示的状态。

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

如果在iOS7之前工作,使您的单元格select样式没有

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

否则,由UITableViewCellSelectionStyleDefault保留

然后:

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

此代码将正常工作

将select样式设置为none之后,可以使用以下委托方法:

 -(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; } 

在cellForRowAtIndexPath中使用这个代码:

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

希望这会为你工作。

享受编码:)

为了得到这个工作,你必须将select样式设置为UITableViewCellSelectionStyleNone然后你应该重写方法setSelected:animated:得到你想要的结果。 当您看到蓝色(或灰色)select时,它会执行与iOS自动select机制相同的操作。

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

您也可以通过其他方式来自定义它,例如通过更改UITableViewCell背景等。

在你的UITableViewCell的子类中覆盖下面的函数。

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

为了匹配标准的select风格行为,你需要覆盖setHighlighted:animated:setSelected:animated: setHighlighted: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]; } } 

还要确保select样式 设置为

我能得到它的唯一方法是:

 - (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; }