iOS:UITableViewCell 中带有动态单元格高度的 UITableView

4
我希望在UITableViewCell中实现UITableView,并根据内部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 
} 

enter image description here


请展示您的“prototype-cell”图像。 - dahiya_boy
你是否正在尝试实现可展开的tableView? - iDeveloper
不,它不能扩展...举个例子,我想创建一个食品订单历史记录,其中可以在单元格(内部表)中具有多个食品项目,而主表可以具有多个订单。 - PPreeti
我正在尝试在以下函数中进行操作: 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 } - PPreeti
你是否在使用自动布局来处理表格视图? - Nidhi Patel
显示剩余2条评论
4个回答

0
请使用以下代码:
@IBOutlet weak var tableView: UITableView! //connect the table view

//do following in viewDidLoad method
tableView.estimatedRowHeight = 100 //Your estimated height
tableView.rowHeight = UITableViewAutomaticDimension

同时,您还可以从Storyboard的属性检查器中设置UILable的以下两个属性:

  • 将行数设置为0
  • 将换行方式设置为自动换行

或者您也可以通过代码来设置这些属性:

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

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

不,即使单元格大小不同也有用。我已经实现了它,并且它根据textView的大小工作。如果数据较少,则会收缩,如果数据较多,则会增加其高度。 - Rocky Balboa
这是一个属性,因此它在您的VC加载时首先被调用,但是每次生成时都会调用委托方法。如果您使用新高度重新加载单元格,那么此属性将不会被调用,但已经设置。是的!它也可以工作,毫无疑问,但用户交互性能有所不同。 - dahiya_boy
我正在从API获取数据,因此我也使用了tableView reload。虽然它对我有用。起初,我习惯在tableView.reloadData()行后面放置,但当我在viewDidLoad中使用相同的代码时,它也可以正常工作。这里的UITableViewAutomaticDimension意味着它始终根据tableViewCell内容视图中设置的约束接受自动高度。 - Rocky Balboa
抱歉?你是什么意思? - Rocky Balboa

0

你需要像这样做:

  1. 在UI中,Master-Item#是你的分区标题。因此,取一个UILabel并将其添加为分区标题。

  2. 你的子项是包含一个标签的prtotype-cell单元格。

单元格内标签的约束条件。

  • 在你的cell中首先添加UIView,并按以下方式设置约束:
    1. 顶部、底部、前导、底部到superView0
  • 现在添加标签,并按照以下方式设置约束。
    1. 顶部、底部、前导、底部到UIView8
    2. 根据您的要求给出高度约束。
    3. =>=给出高度关系。
    4. 从storyboard设置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的delegatedataSource


我没有使用自动布局,设计模式是缩放。 - PPreeti
@PPreeti 首先,在 heightForRow 方法中,您不能使用 dequeue 函数。请在 cellForRow 方法中使用它。其次,请将您的代码添加到问题中,以便我们了解您实际上正在做什么。 - dahiya_boy

-2

在viewDidLoad()中使用此代码

self.tableView.estimatedRowHeight = 350
 self.tableView.rowHeight = UITableViewAutomaticDimension

请使用这个委托。
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return UITableViewAutomaticDimension

        }

@PPreeti 你是否使用自定义单元格? - IOS Singh

-3
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
  {
        self.tableView.estimatedRowHeight = 160
        return UITableViewAutomaticDimension
  }

同时设置UILable的这两个属性。

 @IBOutlet weak var label: UILabel!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接