UITableViewCell自定义类 - 在子视图高度约束中更改后重新加载单元格高度 [英] UITableViewCell custom class - Reload cell height after change in subview height constraint

查看:1027
本文介绍了UITableViewCell自定义类 - 在子视图高度约束中更改后重新加载单元格高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UITableViewCell 类中有一个按钮。它显示/隐藏视图(同一单元格的一部分)。单元格的高度应该在这样做时改变。

这是按钮动作(在 UITableViewCell 自定义类中):

I have a button in custom UITableViewCell class. 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):

@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()
    }  

单元格的高度保持不变。只有当我滚动UITableView并重新访问单元格时,它的高度才会更新。

The height of cell remains unchanged. It is only when I scroll the UITableView and revisit the cell, it's height gets updated.

推荐答案

将以下方法添加到<在c / c> c $ c

Add the following method to the viewController with the tableView to refresh the table when a cell is expanded:

func refreshTableAfterCellExpansion() {
    self.tableView.beginUpdates()
    self.tableView.setNeedsDisplay()
    self.tableView.endUpdates()
}

在更新约束后调用方法。如果按钮操作单元格内的约束,则需要通知 viewController 有关更改,以便您可以调用该方法。使用委托模式(或直接传递 tableView 引用每个可扩展单元格 - 只记得将其存储为 weak 变量),或使用 NotificationCenter.default 发布通知。

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. 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.

这篇关于UITableViewCell自定义类 - 在子视图高度约束中更改后重新加载单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆