iOS:UITableView在具有动态单元格高度的UITableViewCell内

iOS:UITableView在具有动态单元格高度的UITableViewCell内,ios,iphone,swift,Ios,Iphone,Swift,我想实现UITableViewinsideUITableViewCell,根据insideUITableView内容大小动态调整单元格高度。我该如何实施这些建议? 我想要这样的布局 代码工作: func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let identifier = "OrderHistoryTVCell" let cell: OrderH

我想实现
UITableView
inside
UITableViewCell
,根据inside
UITableView
内容大小动态调整单元格高度。我该如何实施这些建议? 我想要这样的布局

代码工作:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let identifier = "OrderHistoryTVCell" let cell: OrderHistoryTVCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? OrderHistoryTVCell 
    let tableInnerContentSize = cell.tableInner.contentSize.height 
    let nn = cell.viewAfterTable.frame.height+tableInnerContentSize 
    return nn 
} 
使用以下内容:

@IBOutlet weak var tableView: UITableView! //connect the table view

//do following in viewDidLoad method
tableView.estimatedRowHeight = 100 //Your estimated height
tableView.rowHeight = UITableViewAutomaticDimension
还可以从情节提要的属性检查器部分设置UILable的以下两个属性:

  • 行到0
  • 换行到换行
或者,也可以从代码中设置这些属性:

self.lblxyz.numberOfLines = 0
self.lblxyz.lineBreakMode = .byWordWrapping

注意-在表格视图单元格中正确添加约束

在viewDidLoad()中使用此代码

使用此委托

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return UITableViewAutomaticDimension

        }
还可以设置UILable的这两个属性

 @IBOutlet weak var label: UILabel!
  • label.numberOfLines=0

  • 你必须这样做:

  • 在ui
    Master Item#
    中,它是您的节标题。因此,使用UILabel并将其添加为节标题

  • 您的子项是包含一个标签的
    prtotype单元格
    单元格

  • 单元格内标签的约束。

    • 单元格中
      首先添加
      ui视图
      ,约束条件如下:
    • 顶部、底部、前导、底部至
      superView
      as
      0
    • 现在添加标签并按如下所示提供约束。
    • 顶部、底部、前导、底部至
      ui查看
      as
      8
    • 根据您的要求提供高度限制
    • 给出从
      =
      =
      的高度关系
    • 从情节提要中将
      label
      属性行设置为0
    在中实现这一行

    //MARK:- TableView
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        return 60 // return header height
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        // return your section header i.e. master item label
    
    }
    
    public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
        return 50;
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return UITableViewAutomaticDimension
    }
    
    public func numberOfSections(in tableView: UITableView) -> Int{
    // return number of section i.e. total count of master item.
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    // returns number of cells in each section
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        // return your custom cell here
        //eg:
        // cell.labelFood.text = your data
    }
    

    注意别忘了绑定tableView的
    委托
    数据源

    向我们展示您的
    原型单元格
    图像。我已更新了此处的所需视图…您是否尝试实现可扩展tableView???不…它不可扩展。。。例如…我想创建食品订单历史记录,它可以在单元格(内部表格)中有多个食品项目,主表格可以有多个订单。请让我知道…我应该在哪里使用单元格内部表格视图的委托方法。。。在UITableviewCell类中或在ViewController中?否。即使单元格大小不同,也会有帮助。我已经实现了它,它根据textView的大小工作。如果它有更少的数据,它将收缩,如果它有更多的数据,它将增加它的高度这是属性,所以它在VC加载时第一次调用,但每次生成时都调用委托方法。如果重新加载新高度的单元格会怎么样。此时此属性未调用,但已设置。是 啊毫无疑问,它也能工作,但在用户操作性能上存在差异。我从api获取数据,所以我也使用tableview重新加载。虽然它对我有用。首先,我习惯于在tableView.reloadData()行后面加上,但当我在viewDidLoad中使用相同的行时,它也可以工作。此处,UITableViewAutomaticDimension表示它始终根据在该tableViewCell的内容视图中设置的约束接受自动高度。抱歉?你是什么意思?我没有使用自动布局,设计图案是缩放。@p请首先在
    heightForRow
    方法中你不能
    dequeue
    单元格。只能在
    cellForRow
    方法中执行此操作。第二,添加有问题的代码,让我们看看您实际在做什么。@p请看您是否使用自定义单元格?
     @IBOutlet weak var label: UILabel!
    
    //MARK:- TableView
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        return 60 // return header height
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        // return your section header i.e. master item label
    
    }
    
    public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
        return 50;
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return UITableViewAutomaticDimension
    }
    
    public func numberOfSections(in tableView: UITableView) -> Int{
    // return number of section i.e. total count of master item.
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    // returns number of cells in each section
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        // return your custom cell here
        //eg:
        // cell.labelFood.text = your data
    }