Ios cell.contentView系统布局大小设置大小:不适用于动态高度表视图

Ios cell.contentView系统布局大小设置大小:不适用于动态高度表视图,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我尝试在自定义uitableviewcell中使用自动布局 并尝试根据本课题实现动态高度 但我的单元格是由xib文件创建的。所以方法有些不同 这是我在HeightForRowatineXpath中的代码 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { myDataItem *item= [[data items] objectAtIndex

我尝试在自定义uitableviewcell中使用自动布局 并尝试根据本课题实现动态高度

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

   -(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; }
我从[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]得到的高度总是“零” " 当我尝试调试时,我发现customcell的所有子视图都是“nil”,但单元格本身不是“nil”。这可能是因为我的customcell是由nib创建的,当我执行[[customcell alloc]init]时,单元格没有完全创建

但我确实尝试将方法更改为cell=[\u tableView dequeueReusableCellWithIdentifier:@“CustomCell”]

结果仍然是一样的。每个子视图都是零。这会使高度返回到零,并使我的tableView显示不正确。有什么办法解决这个问题吗

我试着在AutoLayout上做这些。事实上,在上一个版本中,我没有使用AutoLayout并手动计算单元格高度。它确实非常有效。无论如何,我只想尝试AutoLayout

请帮忙。提前谢谢你这是我的解决方案 我通过分配新的细胞 NSArray*topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@“CustomCell”所有者:自选项:nil]; //抓取指向第一个对象的指针(可能是自定义单元格,因为这是XIB应该包含的全部内容)


据我所知,在iOS 8下,建议的解决方案不适用于具有多行标签的单元格,因为
systemLayoutSizeFittingSize
将不考虑所需的单元格宽度(简单地说,它将尝试将标签布局成一行,从而使单元格比它应该的宽度更宽)


我在单元格的
contentView
中安装了一个宽度约束,而不是设置单元格边界,从而解决了这个问题。请记住在调用
systemLayoutSizeFittingSize:
单元格=[CustomCell alloc]后立即卸载这个约束init];如果您打算从nib加载单元格,则无法工作。请查看有关如何执行此操作的信息。谢谢。我现在可以从xib创建自定义单元格。但是[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]仍然返回nil该方法的返回类型为CGFloat,而不是id,因此它不应该返回nil。但是,如果它返回0.0,则约束可能有问题。同样,我建议在代码中执行约束,因为这样更易于调试和正确执行。抱歉,我是指零。Cell.contentView systemLayoutSizeFittingSize不起作用,但当我改为cell.commentView时,它确实返回了有效值。因为comment视图是唯一具有动态高度的视图,所以我使用其返回值来计算Row的高度。我也面临着同样的问题,你能解决这个问题吗?
返回高度+1
用于分隔符。更灵活的方法是
CGFloat分隔符=((UITableViewCellSeparatorStyleNone!=self.tableView.separatorStyle)?1:0);返回高度+分隔符;}
        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; }