【问题标题】:UITableView cells added with insertRowsAtIndexPaths are not drawn/laid out correctly使用 insertRowsAtIndexPaths 添加的 UITableView 单元格未正确绘制/布局
【发布时间】:2016-11-12 13:23:24
【问题描述】:

我有一个UITableView,它分为多个部分,其中一些我正在尝试使其可折叠 - 点击一个部分的标题,该部分中的单元格将展开/折叠。

我在 self.tableView.beginUpdates()self.tableView.endUpdates() 块内使用 tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) 执行此操作。

第一次打开一个部分时(即第一次向该部分添加行),里面的单元格布局不正确。代替多行文本和高度扩展以适应所需内容的单元格,我得到一个标准 44pts 高的单行单元格,其文本被省略号截断。

单元格的高度通常由estimatedHeightForRowAtIndexPath 控制,返回UITableViewAutomaticDimension

当单元格滚动到屏幕外,或者关闭并重新打开某个部分时,单元格似乎会重新绘制并显示其正确的布局。

那么有没有办法让单元格/部分/表格在新单元格被动画显示之前重新绘制自己?

我尝试过self.tableView.reloadData()reloadRowsAtIndexPaths 之类的方法均未成功,但我可能没有正确使用这些方法。

我认为我的代码是正确的,但它可能在“渲染序列”中的位置错误,因为需要一个更好的术语。

非常感谢!

【问题讨论】:

    标签: ios iphone uitableview swift


    【解决方案1】:

    我只是遇到了同样的问题,并且能够通过为 tableView 设置一个估计的RowHeight 来解决这个问题。默认值为 0,因此将其设置为正值似乎可以解决问题:

    tableView.estimatedRowHeight = 44.0;

    【讨论】:

      【解决方案2】:
      self.mainTableView.beginUpdates()
          self.mainTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Fade)
          self.mainTableView.reloadRowsAtIndexPaths(self.mainTableView.indexPathsForVisibleRows!, withRowAnimation: UITableViewRowAnimation.Fade)
          self.mainTableView.endUpdates()
      

      这对我来说很完美。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多