0

So I have looked over this answer. But I am still not getting the heights to return correctly for iOS7. I have a table view with a cell that I laid out in autolayout that is fairly complicated. Whenever I try to override the heightForRowAtIndex (to make it iOS7 compatible) I get row heights that don't seem to be calculated correctly. When I don't override heightForRowAtIndex everything works great, except that it doesn't work on iOS7. I am also trying to layout the whole thing automatically using xibs. There isn't really any logic in my DipticCell.m. It's all in the UITableView subclass.

-(id)init{
    //Implementation details go BELOW
    self.dataSource = self;
    self.delegate = self;
    [self registerNib:[UINib nibWithNibName:@"DipticCell" bundle:nil] forCellReuseIdentifier:@"dipticCell"];
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.rowHeight = UITableViewAutomaticDimension;
    offScreenCells = [[NSMutableDictionary alloc] init];
    return self;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *reuseIdentifier = @"dipticCell";
    if (indexPath.row > 0){
        reuseIdentifier = @"cell2";
    }

    UITableViewCell *cell = [offScreenCells objectForKey:reuseIdentifier];
    if (!cell) {
        cell = [self dequeueReusableCellWithIdentifier:reuseIdentifier];
        [offScreenCells setObject:cell forKey:reuseIdentifier];
    }


    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];


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


    [cell setNeedsLayout];
    [cell layoutIfNeeded];

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

    height += 1.0f;
    return height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        DipticCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dipticCell"];
        if (cell == nil) {
            cell = [[DipticCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dipticCell"];
        }
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"];
        }
        cell.textLabel.text = @"one";
        return cell;
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

Just to confirm that everything is in the contentView.

enter image description here

Also, when I override the heightForRowAtIndexPath I get an "Unable to simultaneously satisfy constraints." warning. But when I don't override that then things seem to work (which makes sense because the cell should just adapt to the height of the content and now I'm trying to override it with a height of 1.)

Here is a link to the project if you want to run it. AutoLayoutCells

Community
  • 1
  • 1
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131

1 Answers1

0

What worked for me most of the time is to make sure, that you have a constraint path going from the top of the cell down to the bottom of the cell.

So if your cell as three labels: label1, label2, label3 and all shall be displayed below each other. Than add constraints:

cell.top to label1.top
label1.bottom to label2.top
label2.bottom to label3.top
label3.bottom to cell.bottom

that way the height of your cell is defined by constraints. So make sure to connect all your subviews that define the height of the cell in such a chain.

fabianfett
  • 709
  • 7
  • 15
  • I double checked and everything looks fine. Like I said, it works when I take out the heightForRowAtIndexPath bit, but when I add that back in it estimates the height incorrectly. – Chase Roberts Sep 25 '14 at 18:27