【问题标题】:Swift tableView.reloadRows deleting cellsSwift tableView.reloadRows 删除单元格
【发布时间】:2017-02-03 03:53:51
【问题描述】:

要求;

我有一个静态的UITableView,当单击其中一个单元格时,我试图打开一个pickerview。我用heightForRowAt 更改大小,所以当单击cell 时,我需要reload 以更改大小。如果我使用reloadData(),它会完美运行(我想使用reloadRows,所以我可以添加动画

问题:

当我尝试reloadRows(at..., with: .automatic) 时,行就会消失。如果我 reloadRowsviewWillAppear 中没有任何变化,则该行不会消失。

代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath)
    if colorCelllExpandedIndex != nil{
        let prev = colorCelllExpandedIndex
        colorCelllExpandedIndex = nil
        tableView.reloadRows(at: [prev!], with: .automatic)
        //tableView.reloadData()
    } else {
        colorCelllExpandedIndex = indexPath
        colorCell.frame.size.height = CGFloat(colorCellExpandedHeight)
        tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
    }
    
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    if indexPath == colorCelllExpandedIndex {
        return CGFloat(colorCellExpandedHeight)
    } else {
        if indexPath.row == 1 {
            return CGFloat(colorCellRegularHeight)
        }
        return UITableViewAutomaticDimension
    }
}

屏幕图像:

【问题讨论】:

    标签: ios iphone swift uitableview swift3


    【解决方案1】:

    似乎reloadRows(at:with:)等函数在使用静态UITableView时出现了一些问题,在这个SO question中也有类似的问题没有解决方案。

    但是,我可以通过实现以下函数来更新单元格高度:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    因此,您可以采取以下方法:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath)
        if colorCelllExpandedIndex != nil{
            colorCelllExpandedIndex = nil
        } else {
            colorCelllExpandedIndex = indexPath
        }
        tableView.beginUpdates()
        tableView.endUpdates()
    
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath == colorCelllExpandedIndex {
            return CGFloat(colorCellExpandedHeight)
        } else {
            if indexPath.row == 0 {
                return CGFloat(colorCellRegularHeight)
            }
            return UITableViewAutomaticDimension
        }
    }
    

    这受到another SO question 中提供的关于在静态表格视图中隐藏单元格的答案之一的启发。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      相关资源
      最近更新 更多