Search code examples
iphoneiosuitableviewlayoutsubviews

How to access UITableViewController variable from within a subclass'ed UITableViewCell?


How do I access UITableViewController parameter from within a subclass'ed UITableViewCell?

I have a parameter in the UITableViewController for the font size (i.e. users can change font size). So the layoutSubviews method in my custom subclassed UITableViewCell will need to access the latest font when it needs to re-layout itself (as it label positions will depend on the font).

So my question, from with my custom subclassed UITableViewCell, and specifically within the layoutSubviews method, how do I access the "uiFont" parameter which is an instance variable from the UITableViewController?


Solution

  • Accessing the UITableViewController object from within your cell isn't a good approach in terms of design. What you should be doing is creating an ivar in the table cell itself to store the UIFont object:

    @interface CustomCell : UITableViewCell {
        UIFont *font;
    }
    @property (nonatomic, retain) UIFont *font;
    

    And then in your tableView:cellForRowAtIndexPath method, set the font of the cell:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
        [cell setFont:uiFont];
        ...
    }