登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [ios/uikit]设置UITableViewCell背景色的三种方法     [回主站]     [分站链接]
[ios/uikit]设置UITableViewCell背景色的三种方法
clq
浏览(359) - 2024-02-28 23:59:07 发表 编辑

关键字:

[ios/uikit]设置UITableViewCell背景色的三种方法

实际上应该不只 3 种。

--------------------------------------------------------
https://blog.csdn.net/dongxiaohui2008/article/details/46908387

//方法一:
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];

//方法三:
- ( void )tableView:( UITableView )tableView willDisplayCell :( UITableViewCell )cell forRowAtIndexPath :( NSIndexPath *)indexPath
{
cell .backgroundColor = [ UIColor redColor ];
}

参考:
如何设置UITableViewCell背景色
http://ios.9tech.cn/news/2013/1128/38946.html

--------------------------------------------------------
https://cloud.tencent.com/developer/information/%E6%97%A0%E6%B3%95%E6%9B%B4%E6%94%B9UITableViewCell%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2

无法更改UITableViewCell背景颜色

UITableViewCell是iOS开发中常用的界面元素,用于展示列表数据。默认情况下,UITableViewCell的背景颜色是白色。如果想要更改UITableViewCell的背景颜色,可以通过以下几种方式实现:

使用系统提供的样式:
iOS提供了一些内置的UITableViewCell样式,可以直接使用这些样式来设置背景颜色。例如,可以使用UITableViewCell的backgroundColor属性来设置背景颜色,如下所示:cell.backgroundColor = UIColor.red
自定义UITableViewCell:
可以通过自定义UITableViewCell来实现更灵活的背景颜色设置。首先,创建一个继承自UITableViewCell的子类,然后在子类中重写layoutSubviews方法,在该方法中设置背景颜色,如下所示:class CustomTableViewCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
self.backgroundColor = UIColor.red
}
}
使用UITableViewDelegate的方法:
可以通过UITableViewDelegate的willDisplay方法来设置UITableViewCell的背景颜色。在实现UITableViewDelegate时,重写willDisplay方法,并在该方法中设置背景颜色,如下所示:func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.red
}

以上是几种常见的设置UITableViewCell背景颜色的方法。










总数:0 页次:1/0 首页 尾页  
总数:0 页次:1/0 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1