Reputation: 103
I have a ton of labels & web views in a cell which I want to hide when user is in portrait mode. I tried the following:
for subview in cell.subviews {
print("i am a subview")
if let label = subview as? UILabel {
label.hidden = true
}
}
I get three subviews but those are no UILabels. In general I have >10 Labels which is why this code does not seem to work. Any hints on how to access those Labels in a loop? Thanks...
Upvotes: 1
Views: 268
Reputation: 6114
For myself, I solved pretty same problem by declaring labelsArray: [UILabel]
property in cell and adding pointers to label when creating to this array.
Other way - using tags. Since setting same tag to different views is bad style coding, you can provide some formulas: for example assume your labels will have tags 1000, 1001, 1002 ... . Then you can enumerate all labels by cycle, getting view with tag from 1000 until you will receive nil
Upvotes: 0
Reputation: 307
You are accessing subviews of UITableViewCell but you need to get subviews of contentView of UITableViewCell.
For Example:
cell.subviews[0].subviews
will do.
Upvotes: 1