2

I need to expand or collapse a table view cell and its contents.
For that I'm using NSLayoutConstraints. Though it gets the work done, I'm facing some pesky layout issues with other views in the cell.
Here is a video.
And here is my code:

ViewController:

extension ViewController: TableViewCellDelegate {
    func tableView(shouldExpand cell: TableViewCell) {
        tableView.performBatchUpdates({
            cell.expand()
        })
    }

    func tableView(shouldCollapse cell: TableViewCell) {
        tableView.performBatchUpdates({
            cell.collapse()
        })
    }
}

TableViewCell:

func expand() {
    UIView.animate(withDuration: 0.57) {
        self.viewHeight.constant = 320
        self.view.alpha = 1
        self.layoutIfNeeded()
    }
}

func collapse() {
    UIView.animate(withDuration: 0.57) {
        self.viewHeight.constant = 0
        self.view.alpha = 0
        self.layoutIfNeeded()
    }
}

How to stop the upper view stop resizing?

Koushik Mudi
  • 183
  • 1
  • 12

2 Answers2

1

You can use UIStackView and just show and hide view for collapse and expand. UIStackView will be easy to handle and will work for you.

hites
  • 149
  • 1
  • 7
0

performBatchUpdates is used to animate changes in tableView based on changes in the data source. Since you don't have any change in datasource, there is no need to do the animation inside tableView.performBatchUpdates block. Try it without performBatchUpdates.

func tableView(shouldExpand cell: TableViewCell) {
     cell.expand()
}

func tableView(shouldCollapse cell: TableViewCell) {
     cell.collapse()
}
Jithin
  • 913
  • 5
  • 6
  • Hello @Jithin, thanks for your reply. I tried to run the code outside the block. But the table does not update that way. The alpha values change but the height constraint does not update neither does the cell itself. :( – Koushik Mudi Apr 15 '20 at 08:29
  • https://stackoverflow.com/questions/59280032/expandable-drop-down-inside-tableview-in-swift4/60056546#60056546 @KoushikMudi you can try that answer it will also expand the table please see that screenshot. hope it will help you. – Vipin Pareek Apr 15 '20 at 09:06
  • @VipinPareek Thanks for the comment. Your code obviously will work but that would't animate the frame change for the cell. Table view lays out its subviews when `perfornBatchUpdates` is called. In my video, the upped section needs some modification because the stack view nested in it is losing its actual height. – Koushik Mudi Apr 17 '20 at 08:08