RETableViewManager icon indicating copy to clipboard operation
RETableViewManager copied to clipboard

How does it work with AutoLayout?

Open jianghaoyuan2007 opened this issue 10 years ago • 6 comments

When I using this library set a variable height of cell, what's the best way?

for example:

  • (CGFloat)heightWithItem:(MultilineTextItem *)item tableViewManager:(RETableViewManager *)tableViewManager;

jianghaoyuan2007 avatar Feb 26 '15 09:02 jianghaoyuan2007

I'm assuming you're using custom designed cells?

I just had good success by basically following this guide and looking at the mentioned example project. I've used IB to set to constraints though.

The key points are to implement layoutSubviews of your custom cell class like this

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];

    self.body.preferredMaxLayoutWidth = CGRectGetWidth(self.body.frame);
}

Register your VC as RETableViewManagerDelegate, keep a reference to an offscreen cell obtained with heightCalculationCell = [tableView dequeueReusableCellWithIdentifier:@"TwoLineItem"]; and implement

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    heightCalculationCell.header.text = @"heading";
    heightCalculationCell.body.text = content[indexPath.row];

    [heightCalculationCell updateConstraints];

    heightCalculationCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tv.bounds), CGRectGetHeight(heightCalculationCell.bounds));

    [heightCalculationCell layoutSubviews];

    CGFloat height = [heightCalculationCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height + 1;
}

@romaonthego it would be nice to get the reference to the item passed into the delegate, to be able to react to properties like the accessoryType. What's your opinion about a tableView:heightForRowAtIndexPath:forItem: delegate method which is called instead of tableView:heightForRowAtIndexPath if implemented?

sebastianludwig avatar Feb 27 '15 10:02 sebastianludwig

I just found out that is possible with implementing the class method +heightWithItem:tableViewManager:.

static TwoLineCell *heightCalculationCell = nil;

@implementation TwoLineCell

+ (CGFloat)heightWithItem:(TwoLineItem *)item tableViewManager:(RETableViewManager *)tableViewManager
{
    if (!heightCalculationCell) {
        heightCalculationCell = [tableViewManager.tableView dequeueReusableCellWithIdentifier:@"TwoLineItem"];
    }

    heightCalculationCell.item = item;
    [heightCalculationCell cellWillAppear];
    [heightCalculationCell layoutSubviews];
    [heightCalculationCell updateConstraints];

    heightCalculationCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableViewManager.tableView.bounds), CGRectGetHeight(heightCalculationCell.bounds));

    [heightCalculationCell layoutSubviews];

    CGFloat height = [heightCalculationCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height + 1;
}

sebastianludwig avatar Feb 27 '15 10:02 sebastianludwig

However, to implement row height caching, the heightWithItem solution is not sufficient and we're back to tableView:heightForRowAtIndexPath:forItem:. Fancy a PR @romaonthego?

sebastianludwig avatar Mar 02 '15 00:03 sebastianludwig

@sebastianludwig I tried your code, but failed, I create the cell with pure code not xib. Can you give more code segment?

wayne798 avatar Jan 16 '17 07:01 wayne798

@wayne798 can you elaborate on "failed"? What exactly is the problem? Can you provide a demo project showcasing your issue?

sebastianludwig avatar Jan 16 '17 19:01 sebastianludwig

@sebastianludwig Aha,I resolved it , I changed "heightCalculationCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;" to "heightCalculationCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;" , then it worked well. I googled "systemLayoutSizeFittingSize" , there are some issues like me. I use Xcode8.1 ans iOS10.2

wayne798 avatar Jan 20 '17 08:01 wayne798