Burkart Burkart - 5 months ago 11
Swift Question

Access all UILabel objects in a tableView cell

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...

Answer

You are accessing subviews of UITableViewCell but you need to get subviews of contentView of UITableViewCell.

For Example: cell.subviews[0].subviews will do.

Comments