【问题标题】:Dynamic height for static table cells with wrapping labels?带有包装标签的静态表格单元格的动态高度?
【发布时间】:2012-08-12 10:37:50
【问题描述】:

我的文字在纵向模式下是两行长。当我切换到横向模式时,它适合一行。我通过情节提要使用静态表格视图单元格;如何调整行的大小以使其紧贴?

屏幕是登录屏幕。

  • 第一个单元格包含一些说明文字
  • 第二个是用于输入帐户名称的文本字段
  • 第三个是用于输入密码的安全文本字段
  • 第四个(也是最后一个)单元格包含登录按钮。键盘上的返回键提交表单或根据需要切换焦点

【问题讨论】:

标签: ios uitableview uistoryboard


【解决方案1】:

使用UITableView's heightForRowAtIndexPath

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   int topPadding = 10;
   int bottomPadding = 10;
   float landscapeWidth = 400;
   float portraitWidth = 300;

   UIFont *font = [UIFont fontWithName:@"Arial" size:22];

   //This is for first cell only if you want for all then remove below condition
   if (indexPath.row == 0) // for cell with dynamic height
   {
      NSString *strText = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label  
     if(landscape)//depends on orientation
     {
       CGSize maximumSize = CGSizeMake(landscapeWidth, MAXFLOAT); // change width and height to your requirement
     }
     else //protrait
     {
       CGSize maximumSize = CGSizeMake(portraitWidth, MAXFLOAT); // change width and height to your requirement
     }

     //dynamic height of string depending on given width to fit
     CGSize textSize = CGSizeZero;
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
     {
        NSMutableParagraphStyle *pstyle = [NSMutableParagraphStyle new];
        pstyle.lineBreakMode = NSLineBreakByWordWrapping;

        textSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil] size];
     }
     else // < (iOS 7.0)
     {
        textSize = [strText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping] 
     }

     return (topPadding+textSize.height+bottomPadding) // caculate on your bases as u have string height
   }
   else
   {
       // return height from the storyboard
       return [super tableView:tableView heightForRowAtIndexPath:indexPath];
   }
 }

编辑:为support 添加&gt; and &lt; ios7sizeWithFont 方法在iOS 7.0 中已弃用

【讨论】:

  • 如果我这样做,我需要手动控制每个单元格的高度,而不仅仅是我的第一个,对吗? (这并不可怕,我只想说清楚。)
  • 谢谢。我认为这个调用不适用于情节提要 + 静态单元格,但我已经检查过,你是绝对正确的。
  • 我希望您不介意,但我找到了一种从情节提要中获取其他行高度并将其编辑到您的答案中的方法。
  • 这行得通,顺便说一句,因为虽然 UITableViewController “不应该实现数据源协议”来为静态表视图提供内容,但上面的方法是 delegate的一部分> 协议,它似乎仍然可以用来调整故事板定义的样式和布局。 (引用来自 Apple 的 TableView 编程指南developer.apple.com/library/ios/documentation/userexperience/…
  • 刚刚使用了答案,它可以工作,除了一件小事。 size 不是CGRect 上的方法,您需要使用该属性。所以应该是:strSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil].size];
【解决方案2】:

我已经通过更简单的实现取得了成功。只要您的静态表格视图对单元格有适当的约束,您就可以要求系统为您调整大小:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    let cell = self.tableView(self.tableView, cellForRowAtIndexPath: indexPath)
    let height = ceil(cell.systemLayoutSizeFittingSize(CGSizeMake(self.tableView.bounds.size.width, 1), withHorizontalFittingPriority: 1000, verticalFittingPriority: 1).height)
    return height
}

【讨论】:

  • 这是一个非常古老的问题。在现代 iOS 中,如果您的约束是适当的并且您添加了一个返回 UITableViewAutomaticDimension 的估计函数,您甚至不需要编写 height 方法。 :)
  • 这个答案可能仍然相关。我在 iOS 9 中遇到过实例,其中具有适当约束的静态表格视图在更改标签文本后不会正确动态调整大小。上述解决方法解决了问题,而 UITableViewAutomaticDimension 没有。
  • heightForRowAtIndexPath 内部调用 tableView.cellForRowAtIndexPath 会导致递归
【解决方案3】:

我已经使用适当的约束来实现这一点,其中标签设置了顶部和底部约束,并像这样在 heightForRowAt 中返回 UITableViewAutomaticDimension

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

在我的情况下,我在堆栈视图中有几个标签,我必须将堆栈视图的顶部和底部设置为 contentView 以使单元格增长。

【讨论】:

  • 我会调查的。我认为这可能是 2018 年的正确方法。:) 我知道过去我在自动布局来调整单元格大小方面运气不佳,但这是由于 iOS 9,我终于可以放弃了。
猜你喜欢
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多