How do I set a nested property if the object needs to be cast?
-
19-09-2019 - |
Question
In my method to set labels in my cells (for an UITableView) I'm reading in 'id' that's actually an UITableViewCell since I'm reading in several different types of cells. I.e.:
-(void)setMyCell:(id)cell atIndexPath:(NSIndexPath*)indexPath;
Some of the cells are of the generic UITableViewCellStyle type whilst others are more complicated and load a NIB etc. for the different sections.
To access a UILabel text property for my custom cells I can do something like
[(CustomCell*) cell setMyTextLabel:@"text"];
and I can create a method in the CustomCell class to set the label text.
For the generic cells the label is called 'textLabel'. I think I can do something like
[(UITableViewCell*) cell setTextLabel:<new UILabel here>];
but that seems somewhat messy as I need to create a new UILabel.
I tried
(UITableViewCell*)cell.textLabel.text = @"text";
which would work if cell was of the class UITableViewCell, not id, but it doesn't in this case.
So is there any way to just set textLabel.text without creating a new UILabel?
Solution
Have you tried adding a category to UITableViewCell
with your label-changing method, and then overriding that method in CustomCell
?