Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosizing table view with minimum height cells

I have a project that targets iOS7/8. On iOS7 I do my manual cell height calculations, on iOS8 I let the system do the math.

The cell mainly contains two vertically stacked labels, a title and a description, which are to the left of two decorating views that are unaffected by my problem.

I am trying to get all cells to appear with a height of 60 points, and when they're tapped render them not truncated at full size. This works for most cases. There is an edge case where the title and description do not actually truncate. Tapping the cell on iOS8 in this case shrinks it just the tiniest, annoying little bit.

I'm wondering if there is a way to set a minimum height for a table cell, in addition to setting the estimated row height for my autosizing table in iOS 8?

I hope this makes sense, let me know if I need to explain better. Any help is appreciated as to how I can set a minimum height for iOS 8 autosized cells.

Normal case - truncated title and description / expanded on tap

Truncate title / descriptionExpanded title / description

Edge case - nothing is truncated / expanded on tap actually shrinks the cell

Nothing truncatedNothing truncated expanded

My heightForRowAtIndexPath returns UITableViewAutomaticDimension on iOS 8 and calculates it on iOS7 with a measuring cell. I've also hardcoded the rowHeight property to 60.0f, my expected minimum height, that matches what is laid out in IB.

When the cell is tapped, I call a function to set the cell to expanded. I animate those changes with [tableView beginUpdates].

- (void) setIsExpanded:(BOOL)isExpanded {
    _isExpanded = isExpanded;

    if (_isExpanded) {
        [_descriptionText setLineBreakMode:NSLineBreakByWordWrapping];
        [_descriptionText setNumberOfLines:0];

        [_title setLineBreakMode:NSLineBreakByWordWrapping];
        [_title setNumberOfLines:0];
    } else {
        [_descriptionText setLineBreakMode:NSLineBreakByTruncatingTail];
        [_descriptionText setNumberOfLines:1];

        [_title setLineBreakMode:NSLineBreakByTruncatingTail];
        [_title setNumberOfLines:1];
    }

    [self layoutIfNeeded];
}
like image 936
jlindenbaum Avatar asked Nov 28 '22 23:11

jlindenbaum


1 Answers

As per my comment:

I can't see any difference in the images. I'm also not sure I would know how I would go about testing this without your code.

But one idea, off the top of my head, is to create a constraint in your custom UITableViewCells view of height >= 60.0

Glad this helped.

like image 73
Robotic Cat Avatar answered Dec 10 '22 03:12

Robotic Cat