【问题标题】:UITableViewCell auto height based on amount of UILabel textUITableViewCell 自动高度基于 UILabel 文本的数量
【发布时间】:2017-07-31 17:26:30
【问题描述】:

我的情节提要中有一些棘手的设置,我有一个包含 UITableViewController 的 UIViewController。在 UITableViewController 中,我有几个原型单元已链接到代码中的子类 uitableviewcell 对象。

使用约束和情节提要,我想根据 UILabel 最终的大小来更改原型单元格的高度,这取决于进入其中的文本。

目前我有一个

UIViewController
-- UITableViewController
-- -- UITableViewCell ((melchat) prototype cell)
-- -- -- ContentView
-- -- -- -- UIView ((background) view with drop shadow card type effect)
-- -- -- -- -- UIImageView (Avatar)
-- -- -- -- -- IUlabel (dynamic (depending on code/input) multi line UILabel)

我希望 UILabel 如何调整 UIView(背景)的大小,然后反过来影响 UITableViewCell 的高度。

我正在使用 XCode 8.2.1

我已经截取了情节提要中的布局和应用的约束。

更新

我已经更新了我的约束,几乎都返回到 ContentView 并将 uilabel 行数更新为 0,然后还实现了 UITableViewAutomaticDimension 代码,但它仍然无法正常工作。请参阅下面的代码和屏幕截图。

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

}

【问题讨论】:

  • 删除头像图像视图底部约束并设置标签底部约束还设置行高和估计行高

标签: ios objective-c uitableview storyboard xcode8


【解决方案1】:

进一步了解道的答案..

他是对的,你需要使用UITableViewAutomaticDimension

此外,您需要确保设置约束的方式是单元格中的所有内容都被约束到单元格 contentView。因此,您的标签可能需要诸如

之类的约束
  • ImageView 的主要约束
  • contentView 的顶部约束
  • contentView 的底部约束
  • contentView 的尾随约束

确保将 UILabel 设置为多行(或行 = 0)并且它应该可以工作。

如果您使用 heightForRowAt 委托函数,请确保您返回 UITableViewAutomaticDimension

【讨论】:

  • 我已经更新了我的问题,我可能不明白你的答案.. 因为它不太管用。感谢您的回复。
  • 您对 uiimagview 的标签的约束应该是 label.leading = imageview.trailing + 10 (或您想要的任何距离)。目前它在屏幕截图中看起来仍然设置了高度。还是那张旧截图?
  • 我还在努力。由于某种原因无法使其工作。
  • 好吧,我有点工作了。我现在唯一的问题是我的图像高度很大。所以我要解决这个问题,哈哈
  • 是的,看起来约束条件需要调整,如果你能把你的项目放在github上,我可以看看你是否还在挣扎
【解决方案2】:

使用UITableViewAutomaticDimension

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300

【讨论】:

    【解决方案3】:

    step1: 对 contentView 内部的 UIView 进行以下约束。

    • contentView 的前导约束
    • contentView 的顶部约束
    • contentView 的底部约束
    • contentView 的尾随约束

    第二步:给 UIlabel 提供以下约束

    • UIImageView 的主要约束
    • UIView 的顶部约束
    • UIView 的底部约束
    • UIView 的尾随约束

    步骤3:然后选择IUlabel的Trailing constraint并选择edit选项,然后选择constant并选择greaterThanEqual选项。

    step4:设置标签的numberofline = 0

    第五步:将此代码添加到viewDidLoad()

    yourtableview.estimatedRowHeight = 80.0
    yourtableview.rowHeight = UITableViewAutomaticDimension
    

    【讨论】:

      【解决方案4】:

      Swift 4.2 和 5。如上所述,您可以设置 UITableView 对象属性或

      tblView.rowHeight = UITableView.automaticDimension 
      tblView.estimatedRowHeight = 300
      

      你也可以通过实现 UITableViewDelegate 方法来定义它

      extension ViewController: UITableViewDelegate {
            func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
              return 300
            }
            func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              return UITableView.automaticDimension
            }
          }
      

      【讨论】:

        【解决方案5】:

        填充UILabel并应用约束后,获取标签的高度并将其设置为UIView的高度。

        之后调用 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 并传递 UILabel/UIView 的高度,以便获得该行的实际高度

        【讨论】:

          猜你喜欢
          • 2015-01-01
          • 1970-01-01
          • 2016-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-03
          相关资源
          最近更新 更多