Ramsel Ramsel - 3 months ago 24
Objective-C Question

UITableViewCell setSelected:animated: not working

This is driving me crazy. I've looked here on S.O. for what I thought was a simple answer but couldn't find one.

In my custom

UITableViewCell
:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Configure the view for the selected state
if (selected) {
[self.languageLevelNameLabel setTextColor:[UIColor blackColor]];
}
else {
[self.languageLevelNameLabel setTextColor:[UIColor colorMessageCountZero]];
}

[self setNeedsDisplay];
}


In
tableView:cellForRowAtIndexPath:
of the controller:

if ([level integerValue] == indexPath.row) {

[cell setSelected:YES];
}


I've inserted break points and
selected == YES
is getting passed for the correct cell, and the
if
statement is being executed when it should be, but the text never gets set to
blackColor
.

Answer

If you want to set your cell as selected use the method selectRowAtIndexPath:animated:scrollPosition: like this in your cellForRowAtIndexPath method of your table view

[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];