当我单击我UITableViewCell
的 时,当我单击单元格时,背景部分(我的背景图像未覆盖的区域)变为蓝色。此外,UILabel
单击单元格上的所有 s 都会变成白色,这是我想要的。
但是,当我单击它时我不想要的是蓝色背景,但如果我这样做,那么我会丢失单元格中 sselectionstylenone
的突出显示颜色。UILabel
那么有什么方法可以在单击单元格时摆脱蓝色背景但保留UILabel
s 的突出显示颜色?
当我单击我UITableViewCell
的 时,当我单击单元格时,背景部分(我的背景图像未覆盖的区域)变为蓝色。此外,UILabel
单击单元格上的所有 s 都会变成白色,这是我想要的。
但是,当我单击它时我不想要的是蓝色背景,但如果我这样做,那么我会丢失单元格中 sselectionstylenone
的突出显示颜色。UILabel
那么有什么方法可以在单击单元格时摆脱蓝色背景但保留UILabel
s 的突出显示颜色?
您可以按如下方式执行此操作。将表格单元格的选择样式设置为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];
}
}
如果在 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];
}
}
还要确保选择样式未设置为None。
我可以让它工作的唯一方法是:
- (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;
}
您也可以使用 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;
}];
}
}