具有动态高度和自动布局约束的UITableView
我有一个表格视图,cells
使用自动布局constraints
动态增长。我有一个场景,我必须在这些动态构建的单元之一中显示UITableView
。这里最主要的是我不知道任何东西的框架,我必须使用约束来构造一切。我需要的是解释如何实现这一目标?具有动态高度和自动布局约束的UITableView
所以我想你有一个MasterViewController,它有一个tableView。然后,您想添加另一个属于类MyCustomcell的一部分的tableView,它位于MasterViewController的tableView的一个单元格内。
使用tableView添加customCell并不是什么大问题,但根据要在CustomCell中显示的数据计算和维护MasterViewController的高度有点棘手。如果你只是问如何在tableView中添加CustomCell,那么有许多很好的教程可以通过简单的谷歌轻松获得。
我面临类似的情况,并以下面的方式解决了问题。希望这可以帮助。
1)如果你有一个customCell,你只需要显示文本。然后MasterViewController的tableView的高度可以根据文本的内容自动地自动维护并自动布局。 This tutorial有很好的解释。
2)如果你在一个MasterViewcontroller tableView的单元格中添加tableView,那么它变得棘手。现在MasterViewController的单元高度取决于CustomCell tableview高度的高度。我试图通过在Swift 3中应用不同的方式来处理这种情况,但最终通过以下方式得到了结果: -
class MasterViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var customCell:MyCustomCell? = nil
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! MyCustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
customCell = cell
return cell
}
}
extension MasterViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let aHeight = customCell?.tableView.contentSize.height{
return aHeight
}
return 0
}
}
我必须适用于UITableView(它是在UITableViewCell中)的约束是什么? –
你可以只添加铅,尾迹,顶部,底部到0. – skJosh
我做到了,但tableview的高度现在为零。 –
请通过以下方式:https://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-inside-a-uitableviewcell/ –
访问这些链接可能会帮助你http://stackoverflow.com/questions/26752645/dynamic-uitablecellview-height – Aravi
可能的dyblicate http://stackoverflow.com/q/18527227/2012219 – gbk