UITableViewController中的行重新排序后,UI更新不正确 [英] Incorrect UI update after reordering rows in UITableViewController

查看:85
本文介绍了UITableViewController中的行重新排序后,UI更新不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在对表中的行进行重新排序,UI最终结果不正确.场景如下:

So I was reordering rows in a table, the UI end results were incorrect. Here comes the scenario:

原始表格内容:

a
b
c
d
e

如果我将第0行(当前为a)移动到第4行(当前为e),则看到的最终结果是:

If I move Row 0 (currently a) to Row 4 (currently e), the end result that I am seeing is:

c
d
e
a
a


上下文的一部分:


A little bit of the context:

该表正在读取Realm对象的列表.我确认Realm对象也已正确更新.如果在对行进行重新排序后立即放置tableView.reloadData(),则可以获得正确的UI结果.但是,重新加载数据会给我的应用带来不必要的UI动画.下面是我执行重新排序的代码:

The table is reading a list of Realm objects. I confirmed that the Realm objects are properly updated as well. If I put a tableView.reloadData() right after reordering the rows, I can get the correct UI results. However, reloading data brings in unnecessary UI animation to my app. Below is my code to perform reordering:

let realm = try! Realm()
realm.beginWrite()
let itemToMove = self.itemList?[fromIndexPath.row]
self.itemList?.remove(at: fromIndexPath.row)
self.itemList?.insert(itemToMove!, at: toIndexPath.row)
try realm.commitWrite()


我曾成功地对未读取Realm对象的表中的行进行重新排序.因此,我不确定这是否与Realm有关.我已经研究了很多,但并没有真正看到任何类似的问题.任何帮助将不胜感激!


I had successful experience of reordering rows in a table that was not reading Realm objects. So I am not sure if this is even related to Realm. I have researched quite a bit but not really seeing any similar issues. Any help will be greatly appreciated!

推荐答案

因此,将Realm对象作为表视图的数据源的棘手之处在于,我需要协调Realm对象的更新(通知通知令牌)以及重新排序时的表格视图.

So the tricky thing about Realm objects being the data source of a table view is that, I need to coordinate the update of Realm objects (without notifying the notification token) as well as the table view when reordering happens.

从Realm Swift的官方文档中,可以看到使用 commitWrite(withoutNotifying: [token]) 来更新Realm对象的功能.我一直在努力,直到发现这是最近发布的一项新功能,而我的Realm版本却有些过时.这就是为什么当我键入该功能时,Xcode会抱怨的原因.安装Realm 2.2.0后,该功能立即显示.

From the official Realm Swift documentation, a feature was documented to update Realm objects using commitWrite(withoutNotifying: [token]). I have struggled a bit until I found out this is a new feature released recently and my Realm version was a bit out of date. That's why when I typed in that function, Xcode was complaining. After installing Realm 2.2.0, the function showed up right away.

以下是我用于重新排序行的代码:

Below is my code for reordering rows:

do {
    self.itemList?.realm?.beginWrite()

    self.itemList?.move(from: fromIndexPath.row, to: toIndexPath.row)
    self.tableView.moveRow(at: fromIndexPath, to: toIndexPath)

    try self.itemList?.realm?.commitWrite(withoutNotifying: [self.notificationToken])
} catch let error as NSError {
    fatalError(error.localizedDescription)
}

问题的后续跟进:
表格视图中奇数行的背景色最初设置为灰色(只是一些UI功能).但是,在对行进行重新排序后,将不会更新要移动的行的背景色.我尝试使用tableView.reloadData()tableView.reloadRows().但是最后一行以某种怪异的状态结束了,无法再对其进行编辑.因此,基本上,如果我想再次对表格重新排序,则最后一行将不会显示在编辑"状态/视图中.我仍在努力理解并解决剩余的问题.

A little bit of follow-up of the question:
The background colour of odd rows in the table view were originally set to grey(just some UI feature). However, the background colour of the rows being moved will not be updated after reordering them. I tried with tableView.reloadData() and tableView.reloadRows(). But then the last row somehow ended in a weird state where it cannot be edited any more. So basically if I'd like to reorder the table again, the last row will not show up in the "edit" state/view. I am still trying to understand and to resolve this remaining issue.

这篇关于UITableViewController中的行重新排序后,UI更新不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆