Static UITableViewCell changes during viewWillAppear not reflected in display

I am using some static UITableViewCell‘s configured in the Storyboard to display some setting information.

Some of the other cells should be disabled if one of the other settings is toggled off.

  • disable scrolling in a UITableView (iPhone SDK 3.0)
  • UITableViewCell subclass wrong image in cell or old image bug
  • How can i use custom UITableViewCell and UITableView in same xib
  • Why is UIGestureRecognizer being called on my textField clear button?
  • custom UITableCellView programmatically using swift
  • Can you add a UITableViewController's TableView to another View?
  • In order to put the cells into the proper state, during viewWillAppear I read the settings from NSUserDefaults and then change the cells accordingly.

    - (void) viewWillAppear:(BOOL)animated
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) {
                [self otherCell].alpha = 1.0;
                [self otherCell].userInteractionEnabled = YES;
            }
            else {
                NSLog(@"Changing alpha to 0.3");
                [self otherCell].alpha = 0.3;
                [self otherCell].userInteractionEnabled = NO;
            }
    

    The problem is that when I actually run the program, even though it says in the log that the alpha is changed, the alpha doesn’t actually change. The userInteractionEnabled does seem to stick, but the alpha is left at 1.0.

    It’s not a problem of cell reuse, or cell’s not being instantiated in time, because the other settings can be changed just fine.

    Changing it from cell.alpha to cell.contentView.alpha works, but that is a different setting.

    It seems like all of the settings “stick” except for the alpha setting, which somehow is getting overwritten.

    2 Solutions Collect From Internet About “Static UITableViewCell changes during viewWillAppear not reflected in display”

    I am answering my own question because I was able to solve it.

    First, I tried putting the alpha change in cellForRowAtIndexPath, but that didn’t work either. After a lot of tinkering, I’ve come to the conclusion that UITableViewCell‘s alpha setting is somehow special in that it keeps getting overwritten or set to 1.0.

    I found two fixes:

    First, instead of doing the change in cellForRowAtIndexPath, do it in the UITableViewDelegate method willDisplayCell. For whatever reason, changing the cell’s alpha in this method will actually stick. Of course, if you do it this way you have to re-arrange your logic so that the changes are done on a cell-by-cell basis, i.e.:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell  
                                             forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (cell == [self otherCell]) {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) {
            cell.alpha = 1.0;
            cell.userInteractionEnabled = YES;
        }
        else {
            NSLog(@"Changing alpha to 0.3");
            cell.alpha = 0.3;
            cell.userInteractionEnabled = NO;
        }
    }
    }
    

    As I said, I’m not sure exactly why this works in willDisplayCell but not in cellForRowAtIndexPath. Others seem uncertain also:

    What is -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

    UITableView background with alpha color causing problem with UITableViewCell

    The other solution is to, instead of using the problematic alpha, use another setting which will achieve the same effect. In my case, that was the contentView.alpha and the backgroundColor. For whatever reason, these settings will stick, and you can even set them in viewWillAppear and it will work as expected:

    - (void) viewWillAppear:(BOOL)animated {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) {
                [self otherCell].backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
                [self otherCell].contentView.alpha = 1.0;
                [self otherCell].userInteractionEnabled = YES;
            }
            else {
                NSLog(@"Changing alpha to 0.3");
                [self otherCell].backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.3];
                [self otherCell].contentView.alpha = 0.3;
                [self otherCell].userInteractionEnabled = NO;
            }
    }
    

    The disadvantage to the second approach is that now you are overwriting the Storyboard’s cell color settings, but you could work around that by asking the storyboard for the color if you care about that.

    I’m not sure why cell.alpha is treated differently. Maybe something about the way static cells are implemented.

    You could try to hint the cell should be redrawn after your if { .. } else { .. }, by using setNeedsDisplay:

    [self otherCell setNeedsDisplay]
    

    Based on your comment, how do you get to otherCell?
    Is this a post that might help?