Ios UITableViewCell附件未着色

Ios UITableViewCell附件未着色,ios,uitableview,swift,accessory,Ios,Uitableview,Swift,Accessory,我已经读了很多关于这个话题的问答,但似乎没有一个有效,所以我的问题是 我创建了一个自定义的UITableViewCell,在情节提要中,我要求有一个披露指示器附件。据说染色颜色会改变指示器的颜色,但经过大量研究后,我发现: 单元格的accessoryView映射为一个nil值,因此我不能影响它的颜色 我尝试使用selectedBackgroundView创建accessoryView,如下所示: self.accessoryView = UIView() 显然,它只是创造了一个空白,原来的

我已经读了很多关于这个话题的问答,但似乎没有一个有效,所以我的问题是

我创建了一个自定义的UITableViewCell,在情节提要中,我要求有一个披露指示器附件。据说染色颜色会改变指示器的颜色,但经过大量研究后,我发现:

  • 单元格的accessoryView映射为一个nil值,因此我不能影响它的颜色
我尝试使用selectedBackgroundView创建accessoryView,如下所示:

self.accessoryView = UIView()

显然,它只是创造了一个空白,原来的披露附件消失了。我真的对这一切感到困惑,无法找到一种方法来影响电池附件的颜色。欢迎任何帮助

以下是帮助我和应该帮助他人的东西

任何UIView类型和子类型的“着色颜色”属性都会将其着色设置传播到其层次结构中的子视图。您可以为UITableView设置tintColor,并将其应用于其中的所有单元格

也就是说,并非所有UITableViewCell附件类型都会不幸着色

染上颜色的人:

  • UITableViewCellAccessoryCheckmark
  • UITableViewCellAccessoryDetailButton
  • UITableViewCellAccessoryDetailDisclosure按钮->仅显示详细信息部分
以下内容不会着色:

  • UITableViewCellAccessoryDisclosureIndicator
  • UITableViewCellAccessoryDetailDispositionButton->仅显示Disposition按钮
因此,一般来说,您可以更改UITableViewCell附件的颜色。但是,如果您想更改通常表示切换到另一个视图的灰色箭头,则不可能,它将保持灰色

更改它的唯一方法是实际创建自定义UIAccessoryView。 这里有一个故意分解的实现,以保持清晰。 尽管我相信有更好的方法:

在awakeFromNib()方法的自定义UITableViewCell类中

请注意,这也不能着色。与“选项卡栏项目”相比,它将具有所用图像的颜色,因此您可能需要多个图像来显示选定单元格和未选定单元格。

Extension Swift 3:

    extension UITableViewCell {
        func prepareDisclosureIndicator() {
            for case let button as UIButton in subviews {
            let image = button.backgroundImage(for: .normal)?.withRenderingMode(.
                alwaysTemplate)
            button.setBackgroundImage(image, for: .normal)
        }
    }
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.prepareDisclosureIndicator()
    
}
做吧 swift 3:

    extension UITableViewCell {
        func prepareDisclosureIndicator() {
            for case let button as UIButton in subviews {
            let image = button.backgroundImage(for: .normal)?.withRenderingMode(.
                alwaysTemplate)
            button.setBackgroundImage(image, for: .normal)
        }
    }
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.prepareDisclosureIndicator()
    
}

目标C:

for (UIView *subview in cell.subviews) {
    if ([subview isMemberOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subview;
        UIImage *image = [[button backgroundImageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [button setBackgroundImage:image forState:UIControlStateNormal];
    }
}

以下是Tokuriku对美国恐龙的答案的Objective-C版本:

    UIImageSymbolConfiguration *configuration = [UIImageSymbolConfiguration configurationWithPointSize:15.0f weight:UIImageSymbolWeightUnspecified];
    UIImageView *disclosureView = [[UIImageView alloc] initWithImage:[[UIImage systemImageNamed:@"chevron.right" withConfiguration:configuration] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate]];
    disclosureView.frame = CGRectMake(0, 0, 15, 15);
    disclosureView.tintColor = UIColor.systemYellowColor;
    cell.accessoryView = disclosureView;
您可以使用名为“chevron.right”的系统映像来避免创建自己的映像。这里给出的帧大小似乎给出了一个与原生Apple图像大小相近的图像

另外,对于要使用标准附件类型的其他单元格,请小心将cell.accessoryView设置为nil

    cell.accessoryType = self.somePreference ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.accessoryView = nil;

您可以使用
imageWithRendingMode
UIImageRenderingModeAlwaysTemplate
轻松地为公开图像着色。然后在
UIImageView
(或superview)上使用
tintColor
,谢谢。漂亮、干净、简单。任何想知道如何设置默认蓝色以外的颜色的人。只需为“button”设置tintColor,“button.tintColor=.redColor()”tintColor对我不起作用,但上面的代码确实起作用了。我在UITableViewCell上设置了所需的着色颜色。泽尔科的代码导致披露指示符“继承”该颜色。
    cell.accessoryType = self.somePreference ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.accessoryView = nil;