突出显示 UITableViewCell 时更改 UILabel textColor?
Change UILabel textColor when UITableViewCell is highlighted?
我有一个自定义 UITableViewCell
,有一个 UILabel
和一个 UIImageView
。我想在突出显示单元格时更改背景颜色和文本颜色。在我的 CustomCell
的 setHighlighted
方法中,我有以下代码:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(self) {
if(highlighted) {
self.title.textColor = [UIColor whiteColor];
} else {
self.title.textColor = [UIColor blackColor];
}
//highlight background
UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
bgColorView.backgroundColor = [UIColor blackColor];
[self setSelectedBackgroundView:bgColorView];
}
}
我已经尝试将 textColor
更改的代码放入 tableView
的 didSelectRowAtIndexPath
,但这不是我想要的效果 - 我想要更改文本颜色当用户触摸单元格时 - 而不是触摸时。
有什么建议吗?
您应该使用属性 highlightedTextColor
。设置 tableView:cellForRowAtIndexPath
内单元格的颜色,它应该如下所示:
cell.textLabel.highlightedTextColor = [UIColor blueColor];
如果您有自定义标签,请使用以下代码
lblPrd.highlightedTextColor = [UIColor whiteColor];
试试这个
[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
覆盖方法
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
//TODO set view highlighted color
}
在自定义 tableviewcell 中
如果您有 Storyboard 或 XIB,只需将其设置为 Attributes Inspector > Label > Highlighted
。
我有一个自定义 UITableViewCell
,有一个 UILabel
和一个 UIImageView
。我想在突出显示单元格时更改背景颜色和文本颜色。在我的 CustomCell
的 setHighlighted
方法中,我有以下代码:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(self) {
if(highlighted) {
self.title.textColor = [UIColor whiteColor];
} else {
self.title.textColor = [UIColor blackColor];
}
//highlight background
UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
bgColorView.backgroundColor = [UIColor blackColor];
[self setSelectedBackgroundView:bgColorView];
}
}
我已经尝试将 textColor
更改的代码放入 tableView
的 didSelectRowAtIndexPath
,但这不是我想要的效果 - 我想要更改文本颜色当用户触摸单元格时 - 而不是触摸时。
有什么建议吗?
您应该使用属性 highlightedTextColor
。设置 tableView:cellForRowAtIndexPath
内单元格的颜色,它应该如下所示:
cell.textLabel.highlightedTextColor = [UIColor blueColor];
如果您有自定义标签,请使用以下代码
lblPrd.highlightedTextColor = [UIColor whiteColor];
试试这个
[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
覆盖方法
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
//TODO set view highlighted color
}
在自定义 tableviewcell 中
如果您有 Storyboard 或 XIB,只需将其设置为 Attributes Inspector > Label > Highlighted
。