Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird iOS bug with UITableViewCell and userInteractionEnabled

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!

like image 365
Matt Holgate Avatar asked Jul 25 '12 16:07

Matt Holgate


2 Answers

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;
like image 101
Chris Avatar answered Oct 21 '22 12:10

Chris


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

like image 21
de. Avatar answered Oct 21 '22 13:10

de.