UITableViewCell配件没有着色

时间:2015-01-15 01:27:13

标签: ios uitableview swift accessory

我已经阅读了有关该主题的多个Q& A但似乎没有任何问题,所以这就是我的问题。

我创建了一个自定义的UITableViewCell,在Storyboard中,我要求有一个披露指示器附件。据说tintColor应该改变指示剂的颜色,但经过大量研究后,我发现了这个:

  • 单元格的附件视图映射为零值,因此我不能影响它的tintColor

我尝试使用selectedBackgroundView创建accessoryView,如:

self.accessoryView = UIView()

显然,它只会创建一个空白区域,原始的公开配件会消失。我真的很困惑这一切,无法找到影响细胞配件颜色的方法。任何帮助都会受到欢迎!

3 个答案:

答案 0 :(得分:24)

扩展

extension UITableViewCell {
    func prepareDisclosureIndicator() {
        for case let button as UIButton in subviews {
            let image = button.backgroundImageForState(.Normal)?.imageWithRenderingMode(.AlwaysTemplate)
            button.setBackgroundImage(image, forState: .Normal)
        }
    }
}

斯威夫特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, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.prepareDisclosureIndicator()
}

swift 3:

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];
    }
}

答案 1 :(得分:15)

这对我有所帮助,应该帮助他人。

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

即使并非所有UITableViewCell附件类型都可能被染色。

那些染色的人:

  • UITableViewCellAccessoryCheckmark
  • UITableViewCellAccessoryDe​​tailButton
  • UITableViewCellAccessoryDe​​tailDisclosureButton - >只有细节部分

以下内容没有着色:

  • UITableViewCellAccessoryDisclosureIndicator
  • UITableViewCellAccessoryDe​​tailDisclosureButton - >只有披露按钮

通常情况下,您可以更改UITableViewCell配件的颜色。但是如果你想将通常表示segue的灰色箭头改为另一个视图,那么就没有机会,它会保持灰色。

更改它的唯一方法是实际创建自定义UIAccessoryView。 这是一个有目的地细分的实现,以保持清晰。 虽然我相信存在更好的方法:

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

let disclosureImage = UIImage(named: "Disclosure Image")
let disclosureView = UIImageView(image: disclosureImage)
disclosureView.frame = CGRectMake(0, 0, 25, 25)
self.accessoryView = disclosureView

请注意,这也不能着色。与" Tab Bar Items"相比,它将使用所用图像的颜色。因此,您可能需要为选定的单元格和未选择的单元格显示多个图像。

答案 2 :(得分:0)

这是 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;