Reputation: 755
I have a Settings screen that is a UITableViewController and contains round 20 static(!) cells (4 groups, 5 cells in each group). Every static cell contains a label.
Is there a way to set the text color of all labels without creating an outlet for each label and setting its text color individually?
Upvotes: 3
Views: 1616
Reputation: 850
In my case, I wanted to change an entire section's Alpha and userInteraction based on whether a switch is on of off. the answer from https://stackoverflow.com/a/36097181/14414215 and https://stackoverflow.com/a/36100831/14414215 helped to steer in the right direction to this function.
func updateCellAlpha() {
let section = 1
let numberOfRows = self.tableView.numberOfRows(inSection: section)
for row in 0..<numberOfRows {
if let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: section)) {
for subview in cell.contentView.subviews {
subview.alpha = syncToSwitch.isOn ? 1.0 : 0.5
subview.isUserInteractionEnabled = syncToSwitch.isOn ? true : false
}
}
}
}
Upvotes: 0
Reputation: 755
Here is one more way to do this. This one guarantees that you access all the labels in the cell's view hierarchy, does not matter at what level they are:
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)
}
}
}
Upvotes: 3
Reputation: 755
Here is how I did it:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let view = cell.contentView.subviews.first
if let viewToTest = view as? UILabel
{
viewToTest.textColor = UIColor.redColor()
}
}
Actually in my case the label and other controls in a cell are in UIStackView, so to access the label view I use:
let view = cell.contentView.subviews.first?.subviews.first
instead of:
let view = cell.contentView.subviews.first
because in this case my labels are one level further in the cell.
Upvotes: 0
Reputation: 623
Alternatively implement -tableView:cellForRowAtIndexPath:
, just don't call dequeueReusableCellWithIdentifier:
as that doesn't work on static tableView cells. Call super.cellForRowAtIndexPath:
instead.
Then you can access the label via cell.textLabel
or if custom cell: cell.contentView.subviews.first as? UILabel
Upvotes: 2
Reputation: 985
I would recommend subclassing UILabel, and setting your text color in the subclass. In your layout, change the class of your UILabels to your subclass. As a bonus, make it IBDesignable so you can see your customizations show up in the storyboard.
import UIKit
@IBDesignable
class CustomLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
customize()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func prepareForInterfaceBuilder() {
customize()
}
private func customize() {
textColor = UIColor.redColor()
}
}
Upvotes: 0