【问题标题】:Dynamic table cell height with Autolayout iOS 6 +使用 Autolayout iOS 6 + 的动态表格单元格高度
【发布时间】:2013-01-11 20:09:49
【问题描述】:

我有UITableviewCell 子类。在那个单元格中,我有 2 个标签(lblCommentlblDateTimeStampe)和一个显示评级星的视图。 我希望 lblComment 的动态高度适合所有文本。它应该根据评论的长度扩大和缩小高度。 我之前已经实现了这个,但是 WITHOUT AutoLayout 如下所示

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {  

    NSString *label =  self.userComment.commentText;
    CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999) 
                              lineBreakMode:UILineBreakModeWordWrap];

    return stringSize.height+10;

} 

现在我正在使用自动布局功能。

如何使用 Autolayout 实现这一点?

感谢任何形式的帮助。谢谢

【问题讨论】:

  • 您找到解决方案了吗?如果有,请分享。
  • 我没有得到任何解决方案。所以我最终以编程方式在 cellForRowAtIndexPath 中添加 UI 元素,然后使用上述方法。然后按照我的要求工作。
  • 在这里看看我对这个问题的回答:stackoverflow.com/a/18746930/796419

标签: dynamic uitableview ios6 height


【解决方案1】:

您可以使用免费提供的 Sensible TableView 框架。该框架会随着内容的增长自动调整单元格的大小。如果表格视图已显示,它也会动态执行此操作。

【讨论】:

    【解决方案2】:

    如果您在 IB 中正确设置了约束,这应该可以工作。您不必以编程方式添加元素,尽管正如您所说的那样也可以。

    假设您在 tableviewcell 中有 label1(可变高度)、label2 和 view1,您应该:

    1. 在 label2 和 view1 上设置固定高度限制
    2. 将 view1 的底部固定到单元格的底部
    3. 固定view1和label2的垂直间距
    4. 将label2和label1的垂直间距固定
    5. 将 label1 的顶部间距固定到单元格顶部

    只要确保你对 label1 没有高度限制,如果你这样做,它应该只大于或等于。通过这样的配置,您可以继续使用 heightForRowAtIndexPath 并且 label1 将根据单元格的高度垂直扩展和收缩。

    【讨论】:

      【解决方案3】:

      很遗憾,自动布局对tableView:heightForRowAtIndexPath 没有帮助。您仍然必须实现该方法。

      您可以使用UIViewsystemLayoutSizeFittingSize: 方法,但这意味着您必须实例化和配置一个表格视图单元格,这可能会非常昂贵。不过,您可以将其保留在屏幕外并将其重新用于计算。但在这一点上,您并没有真正节省多少开发工作,因此像以前那样手动进行计算可能是最好/最快的方法。

      【讨论】:

      • 在解析数据的时候,我在后台保留了一个UITableViewCell,用它来计算高度,然后把高度值存入对象中。在调用tableView:heightForRowAtIndexPath 时,已经为每个数据对象计算了高度。
      【解决方案4】:

      我已经使用自动布局实现了相同问题的解决方案,并且它有效。

      首先,您需要为 lblComment 定义 heightConstraint。

      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      UIFont *font = [UIFont fontWithName:@"YourFontName" size:YourFontSize] ;
      UITextView *calculationView = [[UITextView alloc] init];
      [calculationView setFont:font];
      [calculationView setTextAlignment:NSTextAlignmentLeft];
      [calculationView setText:lblComment.text];
      int width = 0;
      if(self.appDelegate.isDeviceiPhone)
          width = 284;
      else
          width = 720;
      CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
         return size.height;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
      //initialize the cell..
      
      UIFont *font = [UIFont fontWithName:@"YourFontName" size:YourFontSize];
      
      UITextView *calculationView = [[UITextView alloc] init];
      [calculationView setFont:font];
      [calculationView setTextAlignment:NSTextAlignmentLeft];
      [calculationView setText:cell.lblComment.text];
      
      int width = 0;
      
      if(self.appDelegate.isDeviceiPhone)
          width = 284;
      else
          width = 720;
      
      CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
      
      cell.lblDetailHeightConstraint.constant = size.height;
      
      // the other stuff...
      }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 2016-08-03
        • 2012-08-01
        相关资源
        最近更新 更多