Search code examples
iosuitableviewreloaddata

UItableView reloadData does not set the cell display properties properly


I have a table view, and I am reloading its contents dynamically, for reloading the table contents I am using the following code:

[agendaTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

This works fine, but the problem is I am setting the alpha properties of the table cells in the following manner:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {


    if([cell isKindOfClass:[AgendaItemBlank class]])
        cell.backgroundColor = [UIColor clearColor];

    else if([cell isKindOfClass:[AgendaItem class]])
    {
        cell.backgroundColor = [UIColor whiteColor];

        [cell setAlpha:0.82];

        //set the corner radius so that there is a rounded effect
        [cell.layer setBorderWidth:0.5];
        [cell.layer setCornerRadius:9.0];
        [cell.layer setBorderColor:[UIColor redColor].CGColor];
        [cell.layer setMasksToBounds:YES];

        //set a different color for the selected background
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor redColor]];
        [bgColorView.layer setCornerRadius:9.0];
        [cell setSelectedBackgroundView:bgColorView];
        [bgColorView release];
    }
}

The combination of these lead to the fact that after the reload, the table cells initially become opaque, all the properties take effect but not the alpha property.

The cells regain their alpha property when they are scrolled up and down (basically when they are redrawn).

I have also tried setting the alpha within - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, but that does not have any effect.


Solution

  • try

    [cell setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.82]];
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];