0

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.

Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29
Jyotirmoy
  • 710
  • 6
  • 12
  • Have you verified that willDisplayCell is getting called? Or the order in which it's getting called. My guess is that it is getting called, but before the animation logic for the fade happens, and that logic changes the alpha again. In which case you may have to change your choice of animation to one that doesn't affect the alpha. – Rob Booth May 07 '12 at 22:51
  • Yes it does get called. However I am not sure which animation will prevent affecting the alpha. I tried UITableViewRowAnimationNone, even that affects the alpha. – Jyotirmoy May 08 '12 at 15:16

2 Answers2

0

To setup cell background use intermediate UIView object:

UIView* cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
cellBackView.backgroundColor = [UIColor redColor];
cellBackView.alpha = 0.82;
cell.backgroundView = cellBackView;
onegray
  • 5,105
  • 1
  • 23
  • 29
0

try

[cell setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.82]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
Jason McTaggart
  • 878
  • 1
  • 7
  • 18
  • Thanks, it helped. Its amazing to see this working, whereas the [cell setAlpha:] fails during a reload. – Jyotirmoy May 08 '12 at 15:22