[英]UITableViewCell - setting background view and accessory in the initializer
[英]Setting custom color for background of UITableViewCell accessory
我正在尝试让我的表格视图单元格在选中时显示自定义选中标记图像。 问题是,选中标记图像后面的颜色是灰色,而不是我提供的自定义单元格颜色。
我知道之前曾有人问过这个问题-我已通读并尝试了以下建议:
其中。 总体共识似乎是,更改单元格backgroundView
的颜色会影响附件,但对我而言似乎并非如此。 这是我当前的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[UIImageView alloc] initWithImage:_checkImage];
[self setCellColor:_customColor forCell:cell];
}
- (void)setCellColor:(UIColor *)color forCell:(UITableViewCell *)cell
{
cell.contentView.backgroundColor = color;
cell.backgroundView.backgroundColor = color;
cell.backgroundColor = color;
}
请注意,我的checkImage
具有透明背景。 选择单元格后,单元格的背景将更改为customColor
,但附件上的背景仍为灰色。 SOS!
普遍的共识似乎是,更改单元格backgroundView的颜色会影响附件
那是对的。 问题是您没有这样做。 你是这样说的:
cell.backgroundView.backgroundColor = color;
但是cell.backgroundView
为nil,所以该行毫无意义。 您需要先为单元格提供背景视图,然后才能设置该视图的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.