Is it possible to set a custom cell's textLabel?

ios, objective-c, uitableview

Solution

You sure can! Just implement the getters for the labels to redirect to your custom cell's labels.

- (UILabel *)textLabel {
    return self.myCustomCellTextLabel;
}

- (UILabel *)detailTextLabel {
    return self.myCustomCellDetailTextLabel;
}

For people using Swift:

var textLabel: UILabel? {
    return myCustomCellTextLabel
}

var detailTextLabel: UILabel? {
    return myCustomCellDetailTextLabel
}

Problem

When you use the built-in styles (subtitle, right detail, etc) for `UITableViewCell`s, you can access the text labels very easily with `textLabel` and `detailTextLabel` which are properties on the `UITableViewCell`, no matter which style you choose. I used this to my advantage to implement reusable code that allows me to apply specific styles to all of my static cells. But now I want to convert them all to a custom style cell, but with this style I still will only have two labels. My question is, is it possible to manually set the `textLabel` and `detailTextLabel` properties for a custom cell? If so, I would not have to change my code, I would just have to simply set the label properties. Otherwise, I'm going to have to change all of my code to target each individual label for each individual cell which will be really messy. For an example of what I'm doing, I have a method that accepts in a `UITableViewCell` and in that method I can enable or disable that cell, which changes the labels text colors to black or light gray as appropriate. If I can't access the `textLabel` and `detailTextLabel` properties, I'm going to need to add in if statements to compare the cell parameter to my cell outlets to know which labels I need to change.

Original source