cell.contentView systemLayoutSizeFittingSize:不会为动力高度的tableview工作 [英] cell.contentView systemLayoutSizeFittingSize: does not work for dynamic height tableview

查看:203
本文介绍了cell.contentView systemLayoutSizeFittingSize:不会为动力高度的tableview工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在自定义的UITableViewCell使用自动布局
并试图根据实施动态高度此SO话题

I tried to use autolayout in custom uitableviewcell And tried to implement the dynamic height according to this SO topic

<一个href=\"http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights\">Using自动布局中的UITableView动态单元布局和放大器;可变行高度

不过是由厦门国际银行文件创建我的手机。因此该方法有些不同
这里是我的heightForRowAtIndexPath code

But my cell is created by xib file. So the method is somewhat different Here is my code in 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; }

和我是从[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCom pressedSize]得到了高度常是无

当我试图调试,我发现我的customcell所有子视图是无,而是细胞本身是不是无。这可能是因为矿customCell由笔尖制成,当我做[CustomCell的alloc]初始化]电池没有完全建立。

And the height that I got from [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] is alway "nil" " When I tried to debug ,I found that all subviews of my customcell are "nil" but the cell itself is not "nil". This might because the customCell of mine is made by nib and when I do [[CustomCell alloc] init] the cell is not fully created.

但我没有试图改变的方法对细胞= [_tableView dequeueReusableCellWithIdentifier:@CustomCell];

But I did tried to change method to cell = [_tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

结果还是一样。每个子视图为零。这使得高收益为零和使我的tableView显示不正确。有没有什么办法可以解决这个问题?

The result is still the same. Every subview is nil . This make the height return to be zero and the make my tableView display incorrectly. Are there any way to solve this ?

我试图让这些在自动版式。其实,在previous版本,我没有使用自动版式和计算单元格高度手动。它没有很好地工作。无论如何,我只是想尝试自动版式。

I tried to make these upon AutoLayout .Actually, in the previous version ,that I didn't use AutoLayout and calculate the cell height manually .It did work perfectly. Anyway, I just want to tried AutoLayout.

请帮忙.Thanks提前

Please help .Thanks in advance

推荐答案

下面是我的解决方案
我ALLOC由新细胞
NSArray的* topLevelObjects = [[一个NSBundle mainBundle] loadNibNamed:@CustomCell老板:自我选择:无];
     //取得一个指向第一个对象(presumably的自定义单元格,因为这是所有XIB应包含)。

Here is my solution I alloc new cell by 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];

  -(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; }

这篇关于cell.contentView systemLayoutSizeFittingSize:不会为动力高度的tableview工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆