更改所选 UITableViewCell 子类中的元素 [英] Changing elements in selected UITableViewCell subclass

查看:22
本文介绍了更改所选 UITableViewCell 子类中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序实现了一个自定义 UITableViewCell,并创建了一个自定义背景视图和 selectedbackgroundview,两者都很好用.但是,我在单元格上还有其他几个图像和 uilabels,我想更改它被选中时的颜色.我已经覆盖了以下方法,并且在选择单元格时它会正确记录,但它不会更改图像或文本颜色.

I have implemented a custom UITableViewCell for my application and have created a custom background view and selectedbackgroundview, both of which work great. However, I have several other images and uilabels on the cell that I want to change the color's of when it is selected. I have overwritten the following method and it logs correctly when the cell is selected but it does not change the image or the text color.

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

推荐答案

我有一种预感,在方法的开头将 -setSelected:animated: 发送到 super正在阻止对您的单元格的更改.尝试将调用 super 移动到方法的末尾.您还应该覆盖 -setHighlighted:animated: 否则您的选择会显得滞后.

I have a hunch that sending -setSelected:animated: to super at the beginning of the method is preventing the changes to your cell. Try moving the call to super to the end of the method. You should also override -setHighlighted:animated: or your selection will appear to lag.

这篇关于更改所选 UITableViewCell 子类中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆