Uitableview 基于内容的Tableview高度
我想创建一个表视图,该表视图被约束为具有上、左、右边距的超级视图,但其下边距较低。这样,如果故事视图的内容比其超级视图高,它将被限制在底部边距并滚动。但是,如果只有3个单元格,并且这些单元格有其自身的内部一致约束,则tableview的高度将为200 pts,其与super view底部边距之间的空间将为200 pts。到目前为止,在尝试此操作时,如果我没有指定底部约束或将其设置为LessThanoRequire,我将在可视化调试器中看到不明确的布局警告。基于UITableViewAutomaticDimension的内容的Tableview高度Uitableview 基于内容的Tableview高度,uitableview,Uitableview,我想创建一个表视图,该表视图被约束为具有上、左、右边距的超级视图,但其下边距较低。这样,如果故事视图的内容比其超级视图高,它将被限制在底部边距并滚动。但是,如果只有3个单元格,并且这些单元格有其自身的内部一致约束,则tableview的高度将为200 pts,其与super view底部边距之间的空间将为200 pts。到目前为止,在尝试此操作时,如果我没有指定底部约束或将其设置为LessThanoRequire,我将在可视化调试器中看到不明确的布局警告。基于UITableViewAutomat
//make bottom margin constraint @IBOutlet for tableView
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var tableView: UITableView!
var rowCount = 3
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
将插座连接到tableView底部约束
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
var height: [CGFloat] = [0]
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let indexRowNo = indexPath.row
//will give you cell height for indexPath
let frame = tableView.rectForRow(at: indexPath)
//if cell height already exists in height update value else append
if self.height.count-1 >= indexRowNo{
height.remove(at: indexRowNo)
height.insert(frame.size.height, at: indexRowNo)
}else{
height.append(frame.size.height)
}
if indexPath.row == (tableView.indexPathsForVisibleRows?.last)?.row {
//remove old cell heights if now tableView has less noOfCell after reloadData
if self.height.count >= rowCount{
for _ in rowCount..<self.height.count{
self.height.removeLast()
}
}
}
// total Height for tableView
let totalHeight: CGFloat = height.reduce(0, +)
//e.g. - less/greater ThanOrEqualTo bottom margin conditions
let viewTotalHeight = self.view.frame.height - 80
if totalHeight <= viewTotalHeight{
let value = (self.view.frame.height) - (totalHeight + 10)
self.bottomConstraint.constant = value
self.tableView.isScrollEnabled = false
}else{
self.bottomConstraint.constant = 0
self.tableView.isScrollEnabled = true
}
}
func tableView(tableView:UITableView,heightforfooterInstruction节:Int)->CGFloat{
返回CGFloat.leastnormalmagnity
}
变量高度:[CGFloat]=[0]
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexath){
设indexRowNo=indexPath.row
//将为您提供indexPath的单元格高度
让frame=tableView.rectForRow(at:indexPath)
//如果高度更新值else append中已存在单元格高度
如果self.height.count-1>=索引rowno{
高度。移除(位于:indexRowNo)
高度.插入(frame.size.height,at:indexRowNo)
}否则{
height.append(frame.size.height)
}
如果indexPath.row==(tableView.indexPathsForVisibleRows?.last)?.row{
//如果重新加载数据后tableView的noOfCell更少,请删除旧单元格高度
如果self.height.count>=行计数{
对于uu in rowCount..使用UITableView中的自动布局进行动态单元格布局,因为它是动态的,您需要为UITableView提供高度,以便在其中动态生成行。这是不可能的,因为您无权访问UITableView最后一个单元格内容视图。其他每个视图都按照我希望的方式工作。例如,对于堆栈视图,我不必设置为con与高度有关。它将增长到其内容。如果在垂直堆栈视图中有几个标签,并且这些标签有2、3、4行文本,堆栈视图将增长到适当的高度。为什么表视图不能这样做?在具有动态单元格布局的UITableView中,使用原型单元格,每个单元格都有自己的标识符,因此我们使用de排队可重用单元(withIdentifier:
这样我们就可以使用一个单元格,例如100次,因为它是动态的。堆栈视图在此内容中是静态的,但在高度或宽度上是动态的。所以具有动态类型的UITableView不知道您将使用一个单元格的标识符重复使用多少次。但在某些情况下tableview知道吗?它会重新加载数据并拥有所有单元格。)在这一点上,您不能将高度捕捉到它的内容吗?但是在这一点之前,您需要提供UITableView高度,然后您可以在func tableView中手动更改它(uTableView:uTableView,willDisplay cell:
当tableView显示单元格时,它将调用,然后您必须自己计算,例如,如果故事视图的内容高于其超级视图,则滚动启用或更改高度,如是,如果您想知道这将如何工作,请告诉我。