【问题标题】:Setting custom color for background of UITableViewCell accessory为 UITableViewCell 附件的背景设置自定义颜色
【发布时间】:2015-03-15 16:05:32
【问题描述】:

我正在尝试让我的表格视图单元格在选中时显示自定义复选标记图像。问题是复选标记图像后面的颜色是灰色,而不是我提供的自定义单元格颜色。

我知道之前有人问过这个问题 - 我已经阅读并尝试了以下建议:

等等。普遍的共识似乎是改变单元格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,但附件上的背景仍然是灰色的。求救!

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    普遍的共识似乎是改变cell的backgroundView的颜色会影响到附件

    没错。问题是你没有那样做。你说的是:

    cell.backgroundView.backgroundColor = color;
    

    但是cell.backgroundView 为零,所以这条线毫无意义。您需要单元格一个背景视图,然后才能设置该视图的颜色。

    【讨论】:

    • 我改成这个了,还是一样的问题:- (void)setCellColor:(UIColor *)color forCell:(UITableViewCell *)cell { UIView *backgroundView = [[UIView alloc] init]; cell.backgroundView = backgroundView; cell.backgroundView.backgroundColor = color; cell.contentView.backgroundColor = color; cell.backgroundColor = color; }
    • 检查这个。 UIView *view = [[UIView alloc] init]; view.backgroundColor = 颜色; [cell setSelectedBackgroundView:view];
    猜你喜欢
    • 2020-01-06
    • 2020-12-14
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2011-08-22
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多