UITableViewCell附件没有被着色
我已经阅读了关于这个主题的多个问答,但是没有一个似乎起作用,所以这是我的问题。
我创build了一个自定义的UITableViewCell,在Storyboard中,我要求提供一个披露指示器附件。 据说,tintColor应该改变指标的颜色,但经过大量的研究,这里是我发现的:
- 单元格的accessoryView映射到一个零值,因此我不能影响它的tintColor
我试图创build一个像这样selectedBackgroundView完成的accessoryView:
self.accessoryView = UIView()
显然,它只是创造一个空白,原来的披露配件消失。 我真的很困惑这一切,无法find一种方式来影响细胞的配件的颜色。 任何帮助将非常欢迎!
延期
extension UITableViewCell { func prepareDisclosureIndicator() { for case let button as UIButton in subviews { let image = button.backgroundImageForState(.Normal)?.imageWithRenderingMode(.AlwaysTemplate) button.setBackgroundImage(image, forState: .Normal) } } }
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, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { cell.prepareDisclosureIndicator() }
迅捷3:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cell.prepareDisclosureIndicator() }
Objective-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]; } }
这是什么帮助我,应该帮助别人。
任何UIViewtypes和子types的tintColor属性都会将其色调设置传播到其层次结构中的子视图。 你可以设置一个UITableView的tintColor,它将被应用到其中的所有单元格。
也就是说不是所有的UITableViewCell Accessorytypes都可能会被着色。
那些有色人种:
- UITableViewCellAccessoryCheckmark
- UITableViewCellAccessoryDetailButton
- UITableViewCellAccessoryDetailDisclosureButton – >只有细节部分
以下不得着色:
- UITableViewCellAccessoryDisclosureIndicator
- UITableViewCellAccessoryDetailDisclosureButton – >只有披露button
所以一般来说,你可以改变你的UITableViewCell附件的颜色。 但是如果你想把灰色的箭头换成另一个视图,那就没有机会了,它会保持灰色。
改变它的唯一方法是实际创build一个自定义的UIAccessoryView。 这是一个有目的地分解,以保持清晰的实施。 虽然我相信有更好的方法存在:
在我自定义的awakeFromNib()方法中的UITableViewCell类中
let disclosureImage = UIImage(named: "Disclosure Image") let disclosureView = UIImageView(image: disclosureImage) disclosureView.frame = CGRectMake(0, 0, 25, 25) self.accessoryView = disclosureView
被警告,这也不能被着色。 它将使用与“标签栏项目”相比所使用图像的颜色,因此您可能需要选定单元格和未选定单元格的多个图像。