2017-08-16 81 views
0

我正在更新约束的CellForRow方法。更新约束后,我应该调用哪个方法,setUpdateConsraint()或layoutIfNeeded()?或者只是返回细胞。无需调用任何方法?更新约束在cellforRow

+0

调用cell.layoutIfNeeded()调用 – karthikeyan

+0

layoutIfNeeded()用于更新的约束。 – shahnilay86

回答

0

致电layoutIfNeeded()并执行方法prepareForReuse您需要重置约束的方法。

例如:

override func prepareForReuse() { 
    super.prepareForReuse() 

    yourConstraint.constant = defaultConstraint 
} 

为什么这是neeeded:当您滚动,因为细胞被重新使用它会随机使用的限制后。

0

cellForRowAt是一个完全可以接受的地方来更新约束,然后简单地返回单元格。你并不需要调用别的:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "SCellA", for: indexPath) as! SCellA 

    // Configure the cell... 

    cell.theLabel.text = "\(indexPath)" 
    cell.theHeightConstraint.constant = indexPath.row % 2 == 0 ? 40 : 100 

    return cell 
} 
+0

当我做这个约束没有得到更新 –

+0

*“约束没有得到更新”* ---你是什么意思?你不*看到*它?你是否收到错误或警告信息?其他一些代码是否也在改变'.constant'?如果您逐步进行调试,您会得到什么样的价值?你需要提供更多的信息。 – DonMag

+0

更新约束常量值后,调用updateConstraint()工作正常。 –