Transparent UITableViewCell flashing background when animating

后端 未结 1 1581
情话喂你
情话喂你 2021-02-06 10:21

I have a UIViewController with a custom background color. On top of it there\'s a UITableView with UITableViewCells that are semi-transpar

1条回答
  •  情话喂你
    2021-02-06 10:41

    1. Create a subclass of UITableViewCell(Such as named: CustomCell), and override the method in CustomCell.m :

      - (id)initWithCoder:(NSCoder *)aDecoder{
          self = [super initWithCoder:aDecoder];
          if(self){
              // Initialization code
              [self setBackgroundColor:[UIColor clearColor]];
              UIView * bgView = [[UIView alloc] initWithFrame:self.frame];
              [bgView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5]];
              [self addSubview:bgView];
          }
          return self;
      }
      
    2. Remove willDisplayCell method in MasterViewController.m

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
      }
      
    3. Change the cellForRowAtIndexPath method in MasterViewController.m

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
          [self configureCell:cell atIndexPath:indexPath];
          return cell;
      }
      
    4. Change the cell's class type in storyboard enter image description here

    Just have a try :D

    enter image description here

    0 讨论(0)
提交回复
热议问题