cell.contentView systemLayoutSizeFittingSize:不适用于动态高度tableview

Kon*_*ool 5 objective-c uitableview ios autolayout

我尝试在自定义uitableviewcell中使用autolayout并尝试根据此SO主题实现动态高度

在UITableView中使用自动布局来获取动态单元格布局和可变行高

但我的单元格是由xib文件创建的.所以方法有些不同这是我在heightForRowAtIndexPath中的代码

   -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
 myDataItem *item= [[data items] objectAtIndex:[indexPath row]];



   CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
    if (!cell) {
        cell =  [CustomCell alloc] init];
        [self.offscreenCells setObject:cell forKey:@"CustomCell"];
      }  



    [cell.commentView setText:item.comment]; 
    [cell.displayNameView setText:item.displayNameOfItemOwner];

    [cell.timeAgoView setText:item.timeAgo];
   [cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]];


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

    [cell setNeedsLayout];
    [cell layoutIfNeeded];


    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height+1; }
Run Code Online (Sandbox Code Playgroud)

我从[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]获得的高度总是"无""当我尝试调试时,我发现我的customcell的所有子视图都是"nil",但是单元格本身不是"nil".可能是因为我的customCell是由nib创建的,当我[[CustomCell alloc] init]时,单元格没有完全创建.

但我确实尝试将方法更改为cell = [_ tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

结果仍然相同.每个子视图都是零.这使得高度返回为零并且使得我的tableView显示不正确.有什么方法可以解决这个问题吗?

我尝试在AutoLayout上制作这些.实际上,在之前的版本中,我没有使用AutoLayout并手动计算单元格高度.它确实工作得很好.无论如何,我只想尝试AutoLayout.

请帮忙.谢谢

Kon*_*ool 1

这是我的解决方案,我通过 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"owner:self options:nil]; 分配新单元格 // 获取指向第一个对象的指针(大概是自定义单元格,因为这就是 XIB 应该包含的所有内容)。

        cell = [topLevelObjects objectAtIndex:0];

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath    *)indexPath
 {
 myDataItem *item= [[data items] objectAtIndex:[indexPath row]];



  CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
if (!cell) {
         NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
 // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

        cell = [topLevelObjects objectAtIndex:0];

    [self.offscreenCells setObject:cell forKey:@"CustomCell"];
  }  



[cell.commentView setText:item.comment]; 
[cell.displayNameView setText:item.displayNameOfItemOwner];

[cell.timeAgoView setText:item.timeAgo];
  [cell.imageView setImage:[UIImage imageNamed:@"image"]];


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

[cell setNeedsLayout];
[cell layoutIfNeeded];


CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height+1; }
Run Code Online (Sandbox Code Playgroud)