xizor
xizor

Reputation: 1614

Changing elements in selected UITableViewCell subclass

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

Any Idea what I am doing wrong?

Update

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

Upvotes: 1

Views: 1401

Answers (1)

Mark Adams
Mark Adams

Reputation: 30846

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.

Upvotes: 5

Related Questions