开发者社区> 问答> 正文

无法设置UITableViewCell的背景颜色

我用下面的代码自定义背景颜色:

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;

但是没有出现任何结果。这行代码就运行正常:
`
cell.contentView.backgroundColor = [UIColor redColor];
`

这是什么问题?

展开
收起
爵霸 2016-03-26 08:47:42 2060 0
1 条回答
写回答
取消 提交回答
  • `
    cell.backgroundView.backgroundColor
    `

    可以用自定义视图完成

    UIView *backgroundViewForCell = [[[UIView alloc] init] autorelease];
    backgroundViewForCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"row-bg.png"]];
    UIView *selectedBackgroundViewForCell = [[[UIView alloc] init] autorelease];
    selectedBackgroundViewForCell.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.2];
    cell.backgroundView = backgroundViewForCell;
    cell.selectedBackgroundView = selectedBackgroundViewForCell;
    2019-07-17 19:15:08
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载