我有一个设置屏幕,它是一个 UITableViewController,包含大约 20 个静态 (!) 单元格(4 组,每组 5 个单元格)。每个静态单元格都包含一个标签。
有没有一种方法可以设置所有标签的文本颜色,而无需为每个标签创建 outlet 并单独设置其文本颜色?
最佳答案
还有一种方法可以做到这一点。这保证您可以访问单元格 View 层次结构中的所有标签,无论它们处于什么级别:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
recursiveSetTextColorForLabelsInView(cell)
}
func recursiveSetTextColorForLabelsInView(inView: UIView) {
for view in inView.subviews {
if let subview = view as? UILabel {
subview.textColor = UIColor.redColor()
}
else {
self.recursiveSetTextColorForLabelsInView(view)
}
}
}
关于swift - 在具有一堆静态单元格的 TableView 中设置所有标签的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096380/