1

I'm using a static table view which contains 3 different cells. And when a button in the first cell is tapped, the height of the first cell should increase. Below is the function called when the button is tapped.

@IBAction func toggleExpandCamera(_ sender: Any) {
    self.shouldShowCameraPreview = !self.shouldShowCameraPreview
    self.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}

And in the table view's delegate heightForRowAt()

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        let expandedHeight: CGFloat = 415
        let collapsedHeight: CGFloat = 115

        if self.shouldShowCameraPreview {
            return expandedHeight
        }
        return collapsedHeight
    } else if indexPath.row == 2 {
        // If already premium, dont show purchases cell.
        if AGTUserDefaultValues.isUserPremium {
            return 0
        }
        // Last cell should be same height as the table view
        return self.tableView.frame.height - (self.navigationController?.navigationBar.frame.height ?? 0)
            - min(UIApplication.shared.statusBarFrame.height, UIApplication.shared.statusBarFrame.width)
    } else {
        return super.tableView(tableView, heightForRowAt: indexPath)
    }
}

I've verified that heightForRowAt() IS getting called, and it is working fine for the other cell heights when toggleExpandCamera() is called. It's just that the first cell that is behaving quite weird. It seems like it disappeared or something. I've attached screenshots down below, before and after expanding.

Before expanding Camera quality cell After expanding cell

On further inspection, it looks like the cell still exist, but still has the same height. The only difference is there's now more space between the two cells. I also found out the alpha value of the cell is 0.

Debug

UPDATE I've tried created a new project, with only the tableview and the function to expand the cell, and still on that project, the same thing happened. If anyone is curious to help I've uploaded the project here.

Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
  • Do you have any constraints that could interfere ? – Yahel Jan 06 '17 at 13:55
  • @Yahel Hmm.. I don't think so. The weird thing is that it also seems to be changing the alpha of the cell, that is why it becomes invisible after expanding. – Chan Jing Hong Jan 06 '17 at 14:26
  • Have you tried changing the height but only by 10 pixels to check if it is moving just a little bit and in what direction ? Maybe it'll give you a clue of what is happening. I can't see any reason for your alpha to change given the code you provide – Yahel Jan 06 '17 at 15:00
  • @Yahel I've tried that, but the same thing happens. From the debug view hierarchy the cell's height doesn't seem to change, but there is now an extra 10 pixel spacing between the two cells. I've uploaded a sample project that shows the exact problem. The link is updated in the question. Thanks! :) – Chan Jing Hong Jan 07 '17 at 03:56

2 Answers2

0

I've tried your project and found that changing :

self.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) 

into :

self.tableView.reloadData()

Works as expected.

Yahel
  • 8,522
  • 2
  • 24
  • 32
0

I figured out the answer. In my toggleExpandCamera() function instead of reloading the table view, I replaced that with:

@IBAction func toggleExpandCamera(_ sender: Any) {
    self.shouldShowCameraPreview = !self.shouldShowCameraPreview
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}

And the cell height animates as expected.

Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41