Chris Williams Chris Williams - 1 year ago 83
iOS Question

Can I programmatically check what style a UITableViewCell is?

While calculating a UITableViewCell's height, it'd be useful if I could tell what style my UITableViewCell is set to. I don't see any properties on the cell itself. Is this possible?

Answer Source

No, Apple does not expose the style property for UITableViewCell. But you have a couple options.

  1. Create your own subclass of UITableViewCell that saves the style to a property when initWithStyle is called.

    @property UITableViewCellStyle cellStyle;
    // ...
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.cellStyle = style;
        return self;
  2. Save the style manually to the cell tag. You can then check the tag on the cell when you're setting the cell height. For example:

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.tag = UITableViewCellStyleValue1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download