Ios UITableViewCell基于UILabel文本量的自动高度

Ios UITableViewCell基于UILabel文本量的自动高度,ios,objective-c,uitableview,storyboard,xcode8,Ios,Objective C,Uitableview,Storyboard,Xcode8,在我的故事板中设置了一个有点棘手的东西,我有一个UIViewController,其中包含一个UITableViewController。在UITableViewController中,我有几个与代码中的子类uitableviewcell对象链接的原型单元格 使用约束和故事板,我想根据UILabel最终的大小更改原型单元的高度,这取决于进入其中的文本 目前我有一个 UIViewController -- UITableViewController -- -- UITableViewCell ((

在我的故事板中设置了一个有点棘手的东西,我有一个UIViewController,其中包含一个UITableViewController。在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 line count更新为0,然后还实现了UITableViewAutomaticDimension代码,但它仍然不起作用。请参阅下面的代码和屏幕截图

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

}


使用
UITableViewAutomaticDimension

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300

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


在调用
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
之后,传递UILabel/UIView的高度,以获得该行的实际高度,从而进一步了解Dao的答案

他是正确的,您需要使用
UITableViewAutomaticDimension

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
此外,您需要确保设置约束的方式是将单元格中的所有内容约束到单元格contentView。因此,您的标签可能需要约束,例如

  • ImageView的前导约束
  • contentView的顶部约束
  • contentView的底部约束
  • contentView的尾部约束
确保将UILabel设置为多行(或lines=0),并且它应该可以工作


如果在委托函数中使用HeightForRow,请确保返回
UITableViewAutomaticDimension
步骤1:对contentView内部的UIView提供以下约束

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
  • contentView的前导约束
  • contentView的顶部约束
  • contentView的底部约束
  • contentView的尾部约束
步骤2:为UIlabel提供以下约束

  • UIImageView的前导约束
  • UIView的顶部约束
  • UIView的底部约束
  • UIView的尾部约束
步骤3:然后选择IUlabel的尾部约束并选择编辑选项,然后选择常量并选择大于等于选项

步骤4:设置标签的
numberofline=0

步骤5:将此代码添加到
viewDidLoad()


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

删除头像图像视图底部约束和设置标签底部约束还设置行高和估计行高我已更新我的问题我可能不理解你的答案。。因为它不太管用。thnks for your response.uiimagview标签的约束应为label.leading=imageview.training+10(或任意距离)。目前,在屏幕截图中看起来仍然有一个高度设置。还是老截图?我还在做。由于某种原因,它无法工作。好吧,我有点让它工作了。。我现在唯一的问题是我的图像高度很大。所以我要解决这个问题,是的,看起来约束需要调整,如果你能把你的项目放在github上,我可以看看你是否还在挣扎