首页 > 解决方案 > 关于单元格高度更新问题的静态 UITableViewCell 动画中的 UIPickerView

问题描述

我有一个静态UITableView,其中我有UIPickerView一个UITableViewCell. 触发didSelectRowAt时,它应该切换具有UIPickerView. 我使用beginUpdates()andendUpdates()来改变单元格的高度。它在展开时看起来正确,但在折叠时,UIPickerView它没有动画并且比单元格更快地折叠。不知道我做错了什么。

我已确保所有视图都设置trueclipToBoundsUITableViewCellContentView (的UITableViewCell)和UIPickerView. 我试图将 beginUpdates/endUpdates 包装在DispatchQueue.main.async. 我UIPickerView正在使用 AutoLayout 并且(前导、尾随、顶部、底部)边缘等于单元格的内容视图。我正在为我的 UI 使用 StoryBoard。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch (indexPath.section, indexPath.row) {
        case (1,1):
            showPicker = !showPicker
            tableView.beginUpdates()
            tableView.endUpdates()
        default:
            ()
        }
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        switch (indexPath.section, indexPath.row) {
        case (1, 1):
            if !showDueDate {
                return 0
            }
        case (1, 2):
            if !showPicker {
                return 0
            }
        case (_, _):
            break
        }
        return super.tableView(tableView, heightForRowAt: indexPath)
}

我希望动画流畅并UIPickerViewUITabeViewCell.

查看 gif 以查看问题

通过 GIPHY

标签: iosswiftuitableview

解决方案


使用self.tableView.reloadRows(at: IndexPath(row: indexPath.row, section: indexPath.section), with: .fade)代替beginUpdates/ endUpdates。当动画表更改时,它将为您提供流畅的动画。


推荐阅读