I just noticed something very strange with the UITableViewCell class on iOS and the userInteractionEnabled property.
It appears that if userInteractionEnabled is set to NO before assigning text to the cell label, then the text is coloured grey. However, setting userInteractionEnabled to NO after the text has been set leaves the text colour as black (see the example code fragment below).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// swap these two lines around, and the text color does not change to grey!
cell.userInteractionEnabled = (indexPath.row % 2) == 0;
cell.textLabel.text = @"Hello";
return cell;
}
This is really annoying, because it means that I end up with a different behaviour in the case that a cell is reused. The above example demonstrates this - the first page of the table shows alternate rows with grey/black text. Scroll further down so that cells get reused, and you can see that things go wrong.
I just wondered if I am doing something wrong, or if this is an iOS bug? I see the problem under iOS 5.1 on the iPad 3. Any insight really appreciated!
I found that if I put cell.textLabel.textColor = [UIColor blackColor];
right before cell.userInteractionEnabled = NO;
, it seems to fix the problem. This is how it is working on iOS 6.0.1
cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = NO;
I think I found a more convenient workaround for this problem (which I consider to be a bug):
Set the enabled
property on textLabel
and detailTextLabel
manually like this:
cell.userInteractionEnabled = (indexPath.row % 2) == 0;
cell.textLabel.enabled = cell.isUserInteractionEnabled;
cell.detailTextLabel.enabled = cell.isUserInteractionEnabled;
This led me to the answer: https://stackoverflow.com/a/13327632/921573
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With