malhobayyeb
malhobayyeb

Reputation: 2883

UITableView strange layout behavior changes on scroll

I have a UITableView with custom UITableViewCell designed to have automatic height.

The behavior is that in the first load of my UIViewController with UITableView displays my labels with partial texts. Then when I scroll to bottom then scroll to top, I get the desired look for height and for texts.

Here is the screenshots:

Initial look for the view controller:

enter image description here

Here is the look after scrolling bottom then top (This is the correct look for my implementation some Lorem text is removed by me):

enter image description here

Edit 1:

Code:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 101 // much heigher than most cells. But no use.

In the UI Builder, the UITableViewCell's "Row Height" set to "Default" (Custom not checked)

In the UI Builder, the UITableView's "Row Height" set to "101" (also tried to set it to "0" but same result.)

Upvotes: 5

Views: 4845

Answers (2)

Ram Vadranam
Ram Vadranam

Reputation: 485

Getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.

Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.

The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.

If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.

check the link for details:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

Upvotes: 6

Sarvjeet Singh
Sarvjeet Singh

Reputation: 1680

I think you can use heightForRowAtIndexPath method

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    var heightToReturn : CGFloat = 0

    heightToReturn = StringUtil.findHeightForText("your text", havingWidth: "your label width" , andFont: UIFont.systemFontOfSize("font size"))  + // add some buffer for padding

    if heightToReturn < 101
    {
        heightToReturn = 101
    }
    return heightToReturn
    }

}

and use this method to calculate the height of the text

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGFloat result = font.pointSize+4;
if (text) {
    CGSize size;

    CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName:font}
                                      context:nil];
    size = CGSizeMake(frame.size.width, frame.size.height+1);
    result = MAX(size.height, result); //At least one row
}

return result;

}

Upvotes: 0

Related Questions