我为我的应用程序实现了一个自定义UITableViewCell,并创建了一个自定义背景视图和selectedbackgroundview,两者都很好用。但是,我在单元格上有几个其他图像和uilabels,我想改变选择它时的颜色。我已经覆盖了以下方法,并且在选择单元格时它会正确记录但不会更改图像或文本颜色。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
NSLog(@"setSelected:YES");
separatorImage.image = [UIImage imageNamed:@"SeparatorSelected"];
titleLabel.textColor = [UIColor whiteColor];
} else {
NSLog(@"setSelected:NO");
separatorImage.image = [UIImage imageNamed:@"Separator"];
titleLabel.textColor = [UIColor grayColor];
}
}
任何想法我做错了什么?
更新
separatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Separator"]]];
[separatorImage setFrame:CGRectMake(99, 4, 5, 71)];
separatorImage.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:separatorImage];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(111, 4, 188, 26)];
titleLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:21.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:titleLabel];
答案 0 :(得分:5)
我有一种预感,即在方法开头向-setSelected:animated:
发送super
会阻止对您的单元格进行更改。尝试将调用移至super
到方法的末尾。您还应该覆盖-setHighlighted:animated:
,否则您的选择似乎会滞后。