[英]UITableViewCell custom class - Reload cell height after change in subview height constraint
I have a button in custom UITableViewCell
class. 我在自定义UITableViewCell
类中有一个按钮。 It shows/hides a view (part of same cell). 它显示/隐藏视图(同一单元格的一部分)。 The height of cell should change on doing that. 这样做时,单元格的高度应该改变。
This is the button action (in UITableViewCell
custom class): 这是按钮操作(在UITableViewCell
自定义类中):
@IBAction func showHideCartView(sender: UIButton)
{
if sender.tag == 1
{
// Show cart view
self.buttonArrow.tag = 2
self.viewAddToCart.isHidden = false
self.constraint_Height_viewAddToCart.constant = 50
self.buttonArrow.setImage(UIImage(named: "arrowUp.png"), for: .normal)
}
else
{
// Hide cart view
self.buttonArrow.tag = 1
self.viewAddToCart.isHidden = true
self.constraint_Height_viewAddToCart.constant = 0
self.buttonArrow.setImage(UIImage(named: "arrowDown.png"), for: .normal)
}
self.setNeedsUpdateConstraints()
self.setNeedsLayout()
self.layoutIfNeeded()
}
The height of cell remains unchanged. 单元的高度保持不变。 It is only when I scroll the UITableView and revisit the cell, it's height gets updated. 只有当我滚动UITableView并重新访问该单元格时,它的高度才会更新。
Add the following method to the viewController
with the tableView
to refresh the table when a cell is expanded: 将以下方法添加到带有tableView
的viewController
以在扩展单元格时刷新表:
func refreshTableAfterCellExpansion() {
self.tableView.beginUpdates()
self.tableView.setNeedsDisplay()
self.tableView.endUpdates()
}
Call the method after the constraints have been updated. 约束已更新后,调用该方法。 If the button manipulates the constraints inside the cell, you will need to notify the viewController
about the change so that you can call the method. 如果该按钮操纵了单元格内部的约束,则需要将有关更改通知给viewController
,以便可以调用该方法。 Either use a delegate pattern (or pass directly a tableView
reference to each expandable cell - just remember to store it as weak
variable), or post a notification using NotificationCenter.default
. 使用委托模式(或直接将tableView
引用传递给每个可扩展单元格-只记得将其存储为weak
变量),或使用NotificationCenter.default
发布NotificationCenter.default
。
As this code is in the TableViewCell, You need to make a action method for your cell in the TableViewController so when the button is clicked you would know. 由于此代码位于TableViewCell中,因此您需要为TableViewController中的单元格创建一个操作方法,以便在单击按钮时知道。 You can keep a reference of your button in your tableviewCell and then inside the cell cellForRowAt you can do 您可以将按钮的引用保留在tableviewCell中,然后在单元格cellForRowAt中进行操作
cell.yourButtonRef.addTarget(self, action: #selector(expandButtonTapped(_:)), for: .touchDown)
and the make a function called "expandButtonTapped" and add the tableview beign and end update there 然后创建一个名为“ expandButtonTapped”的函数,然后添加tableview beign并在此结束更新
@objc func expandButtonTapped(_ button: UIButton) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
Please let me know if this has worked or not 请让我知道这是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.