Chris Williams
Chris Williams

Reputation: 12481

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?

Upvotes: 2

Views: 1411

Answers (4)

malhal
malhal

Reputation: 30726

For some strange reason Apple didn't include a property in their public header however you can simply call valueForKey to get it:

UITableViewCellStyle style = [tableCell valueForKey:@"tableViewStyle"];

If using valueForKey concerns you, and you are using storyboards instead you could subclass and override:

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        _cellStyle = [aDecoder decodeIntForKey:@"UITableViewCellStyle"];
    }
    return self;
}

The other answers show how to get it for programatically created cells.

Upvotes: 2

DanielG
DanielG

Reputation: 2377

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;
    

Upvotes: 6

tiritea
tiritea

Reputation: 1279

Not directly, or at least not 100% reliably...

When you do [cell initWithStyle:style reuseIdentifier:foo] the chosen style is simply used to (re)layout the contents of the cell, specifically the layout of textLabel vs detailTextLabel. Thereafter the style no longer really matters... But what you can do is subsequently look at the resulting layout - namely the textLabel vs detailTextLabel frames - and deduce from their position & size what the original chosen style probably was [I say 'probably' because if the textLabel or detailTextLabel are nil there's really no way to, say, distinguish a UITableViewCellStyleDefault from UITableViewCellStyleValue1 from UITableViewCellStyleSubtitle, because you just have a textLabel and they all look the same].

That said, something like this may suffice for your needs (after [cell layoutSubviews] that is):

if (!CGRectGetHeight(cell.detailTextLabel.frame) {
    // UITableViewCellStyleDefault, b/c it never shows a detailTextLabel
} elseif (CGRectGetMinY(cell.detailTextLabel.frame) == CGRectGetMinY(cell.textLabel.frame)) {
    // UITableViewCellStyleValue1 or UITableViewCellStyleValue2
} else {
    // UITableViewCellStyleSubtitle
}

Again, YMMV... this assumes your textLabel and detailTextLabel are both non-nil.

BTW You could further distinguish a UITableViewCellStyleValue1 from UITableViewCellStyleValue2 by looking at the labels' textAlignment, but there's really no way of knowing that these weren't re-aligned programmatically sometime after the cell was initWithStyle'd.

Having said all that, if you want to 100% reliably check after-the-fact what style your UITableViewCell was init'd to, then your safest bet is to make a simple subclass which overrides -initWithStyle:reuseIdentifier: and saves this in a new property; ie option 1 of accepted answer.

Upvotes: 0

Chase
Chase

Reputation: 2304

Yes you can. You can access the style method on your tableview to return the current style. Here is a link to apple's documentation.

Edit:

Whoops I just noticed you were asking about a UITableViewCell. Ideally you shouldn't need to know the style of the cell because you can't actually change the style of an existing cell. You can only initialize a cell with a style like this:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];

Upvotes: 0

Related Questions