【问题标题】:Swift - Moving a row within one tableView section causes the headers of other sections to be reloadedSwift - 在一个 tableView 部分中移动一行会导致重新加载其他部分的标题
【发布时间】:2017-10-09 23:56:58
【问题描述】:

这是一个很长的,所以我很感激你的时间!我在重新排序 UITableView 的特定部分中的行时遇到问题。

上下文:我有一个包含多个部分的 UITableView(.plain 样式)。第 0 节显示了很多信息。第 1 部分是喜欢某些内容的用户列表。第 2 部分是另一个不喜欢某些内容的用户列表。第 3 节是用户生成的 cmets 列表。表格中的每个单元格和标题视图都是从 nib 启动的自定义类。我允许用户根据受欢迎程度对 cme​​ts 进行排序。当用户点击排序按钮时,会调用以下函数:

func sortOnPopular() {

    if self.comments.isEmpty {
        return
    }

    self.table.scrollToRow(at: IndexPath(row: 0, section: self.COMMENT_SEC), at: .top, animated: true)

    //FIREBASE method
    //returns a child from a node on the database that is sorted
    //according to the parameter passed to it. The function returns
    //each child one by one according to its rank in the sort.
    _ = ref.child("not really important...").queryOrdered(byChild: "not important also").observe(.childAdded, with: { (snapshot) in

        //Gets the index, i, of the sorted comment in the model, keys.
        if let i = self.keys.index(of: snapshot.key) {
            self.table.beginUpdates()
            self.table.moveRow(at: IndexPath(row:i, section: self.COMMENT_SEC), to: IndexPath(row:0, section:self.COMMENT_SEC))
            self.keys.remove(at: i)
            self.keys.insert(snapshot.key, at: 0)
            self.table.endUpdates()
        }
    })
}

问题:当我在表格视图中只有一个部分时,这段代码可以很好地工作。一旦我添加了多个,当这个函数被调用时,表格视图会做一些奇怪的事情。例如,主要内容显示(第 0 节)似乎会重新加载。用户点击第 0 部分中的排序按钮后,其颜色变为橙色。该按钮将在短时间内保持橙色,但一旦分类完成,它就会恢复为原来的颜色。它应该保持橙色,直到用户再次点击该按钮。我在第 0 节中有一些图像,当调用此函数时,它们似乎也会重新下载。 Section 0的标题背景是视觉效果暗模糊视图;调用此函数时,它不再模糊 - 它似乎是一个稍微透明的黑色视图。此外,当调用此函数时,所有标题都会向上移动,然后返回到它们的原始位置。

基于这一切,我认为这个函数会以某种方式导致表视图重新加载其所有部分和行。

问题:

(1) 调用 tableView.moveRow(at:to:) 是否会重新加载整个 tableView?如果是这样,有没有办法将更新限制在特定部分?

(2) 文档没有说必须使用 tableView.beginUpdates() 和 .endUpdates() ,除非您要插入或删除行。他们在这里有必要吗?

(3) 向 tableView 插入行也会产生上述关于 UIVisualEffectView 渲染的结果。有人有过这种经历吗?

感谢您的耐心等待以及您可以提供的任何建议!

【问题讨论】:

    标签: ios swift uitableview firebase firebase-realtime-database


    【解决方案1】:

    您的代码看起来不错,但是我们可能必须考虑单元格超出视图区域以及我们何时重用单元格的情况。

    我观察到当我们使用移动行时

    tableView.moveRow(at: , to: )

    或者当我们使用

    滚动时

    tableView.scrollToRow(at: , at: UITableViewScrollPosition.top, animated: true)

    在单元重用过程中,结果不一致。

    所以最好使用

    重新加载 cmets 部分

    reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)

    在您对数据进行排序之后。这将重新加载该部分并避免重复使用单元格的麻烦。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2015-07-20
      • 2013-07-30
      相关资源
      最近更新 更多