简体   繁体   English

无法更改静态UITableViewCell高度

[英]Unable to change static UITableViewCell height

I'm using a static table view which contains 3 different cells. 我正在使用包含3个不同单元格的静态表视图。 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() 并在表视图的委托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. 我已经验证了调用了heightForRowAt() ,当toggleExpandCamera()时,它正好适用于其他单元格高度。 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. 我在扩展之前和之后都附加了截图。

在扩展相机质量单元之前 扩展细胞后

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. 我还发现单元格的alpha值为0。

调试

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. 更新我尝试创建了一个新项目,只有tableview和扩展单元格的功能,并且仍在该项目上,同样的事情发生了。 If anyone is curious to help I've uploaded the project here . 如果有人好奇帮助我在这里上传项目。

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. 按预期工作。

I figured out the answer. 我想出了答案。 In my toggleExpandCamera() function instead of reloading the table view, I replaced that with: 在我的toggleExpandCamera()函数中,我没有重新加载表视图,而是将其替换为:

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

And the cell height animates as expected. 细胞高度按预期动态显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM