【问题标题】:Unable to change static UITableViewCell height无法更改静态 UITableViewCell 高度
【发布时间】:2017-05-21 06:32:33
【问题描述】:

我正在使用包含 3 个不同单元格的静态表格视图。当点击第一个单元格中的按钮时,第一个单元格的高度应该增加。下面是点击按钮时调用的函数。

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

在表格视图的委托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)
    }
}

我已经验证 heightForRowAt() 被调用,并且在调用 toggleExpandCamera() 时它在其他单元格高度上工作正常。只是第一个细胞表现得很奇怪。好像消失了还是怎么的。我在下面附上了展开前后的截图。

经过进一步检查,看起来该单元格仍然存在,但仍具有相同的高度。唯一的区别是现在两个单元格之间有更多的空间。我还发现单元格的 alpha 值为 0。

更新 我试过创建一个新项目,只有表格视图和扩展单元格的功能,但仍然在那个项目上,同样的事情发生了。如果有人想帮忙,我已经上传了项目here

【问题讨论】:

  • 您是否有任何可能干扰的限制条件?
  • @Yahel 嗯.. 我不这么认为。奇怪的是它似乎也在改变单元格的 alpha,这就是为什么它在扩展后变得不可见。
  • 您是否尝试过更改高度但仅更改 10 像素以检查它是否移动了一点点以及朝哪个方向移动?也许它会给你一个正在发生的事情的线索。鉴于您提供的代码,我看不出您的 alpha 更改的任何原因
  • @Yahel 我试过了,但同样的事情发生了。从调试视图层次结构来看,单元格的高度似乎没有改变,但现在两个单元格之间多了 10 个像素的间距。我上传了一个显示确切问题的示例项目。问题中的链接已更新。谢谢! :)

标签: ios xcode uitableview static cell


【解决方案1】:

我已经尝试过你的项目,发现改变了:

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

进入:

self.tableView.reloadData()

按预期工作。

【讨论】:

  • 是的,但它不会为更改设置动画:/
【解决方案2】:

我想出了答案。在我的toggleExpandCamera() 函数中,我没有重新加载表格视图,而是将其替换为:

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

并且单元格高度按预期动画。

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多