自定义UITableViewCell的背景颜色,实际上是对cell的contentView的背景颜色进行设置,所以可以有以下方法:

方法一:
cell.contentView.backgroundColor = [UIColor redColor];
方法二:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

 

以上是自定义cell. contentView的背景颜色或view的方法实现cell的自定义背景色,下面有UITableView的UITableViewDelegate方法,也可以实现此效果。代码如下:

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

{

    if (indexPath.row%2==0) {

        cell.backgroundColor = [UIColorwhiteColor];

    }else {

        cell.backgroundColor = [UIColorlightGrayColor];

    }

 

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-04-03
  • 2021-11-16
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2021-05-18
  • 2021-07-06
  • 2021-06-08
  • 2021-09-04
  • 2022-02-06
  • 2021-03-29
相关资源
相似解决方案