2

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

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

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • I have implemented something very similar to yours but all I had to do was " self.tableView.beginUpdates() self.tableView.endUpdates()". Please give that a try – Siyavash Oct 05 '17 at 10:29
  • @Siyavash : I have written the above in UITableViewCell custom class – Nitish Oct 05 '17 at 10:31
  • oh thats probably why then , I will write a possible solution – Siyavash Oct 05 '17 at 10:32
  • @Siyavash : It would be quite easy to do that from UITableViewCell class as well. But I am afraid if that is the right way to do that. – Nitish Oct 05 '17 at 10:36
  • but the reason "The height of cell remains unchanged" until you "scroll the UITableView and revisit the cell" is probably because your tableview is still using the old cell – Siyavash Oct 05 '17 at 10:37
  • Instead of using one cell, you can use 2 cells and put the condition when where you have to show cart cell. – dahiya_boy Oct 05 '17 at 10:42

2 Answers2

5

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

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
1

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. You can keep a reference of your button in your tableviewCell and then inside the cell cellForRowAt you can do

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

@objc func expandButtonTapped(_ button: UIButton) {
    self.tableView.beginUpdates() 
    self.tableView.endUpdates()


}

Please let me know if this has worked or not

Siyavash
  • 970
  • 9
  • 23