Chris Williams Chris Williams - 4 months ago 37
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?


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;