gpt4 book ai didi

ios - 如果在 tableView :cellForRowAtIndexPath 设置 cell.backgroundColor 不响应

转载 作者:可可西里 更新时间:2023-11-01 03:56:22 26 4
gpt4 key购买 nike

我想用红色给某些表格行上色,所以我尝试添加

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... init Code here
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

出现了附属的复选标记,但没有出现红色。

后来我发现将它放在 tableView:willDisplayCell 中就可以了。这是设计使然吗?为什么它会忽略我在初始化单元格时已经设置的值?

最佳答案

单元格的外观在 tableView:willDisplayCell 中自定义。所以,要回答你的问题,是的,这是设计使然。需要在cellForRowAtIndexPath中修改/赋值cell的contentView的附属 View 和 subview 。

来自 tableView:willDisplayCell:forRowAtIndexPath: 的文档:

TableView 在使用单元格绘制行之前将此消息发送给它的委托(delegate),从而允许委托(delegate)在显示之前自定义单元格对象。此方法使委托(delegate)有机会覆盖之前由 TableView 设置的基于状态的属性,例如选择和背景颜色。委托(delegate)返回后, TableView 仅设置 alpha 和 frame 属性,然后仅在行滑入或滑出时设置动画。

关于ios - 如果在 tableView :cellForRowAtIndexPath 设置 cell.backgroundColor 不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8828879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com