Swift UITableView Realm 删除后顺序错误
Swift UITableView Realm wrong order after delete
我在 Realm 中有一个数据库,我将其显示在 UITableView 中,它运行良好。我添加了一个删除功能,如下所示。
if let item = items?[indexPath.row] {
do {
try realm.write {
realm.delete(item)
}
} catch {
print("Error deleting item, \(error)")
}
tableView.reloadData()
}
假设我在数据库中有一些数据,例如 1,2,3,4,5。当我删除“2”时,我希望它像 1、3、4、5 但它是 1、5、3、4。因此最后一个条目分别显示在底部的条目将始终替换已删除的数据。我该如何解决?
Objects is Realm are stored as unordered if you want them in order then you have to apply below code or store it as a List.
let result = realm.objects(ModelClass).sorted("attribute", ascending: true)
- 模型Class 是您的领域对象的名称Class
- 属性是
对象 属性 您想要它们的基础。
希望对您有所帮助。
我在 Realm 中有一个数据库,我将其显示在 UITableView 中,它运行良好。我添加了一个删除功能,如下所示。
if let item = items?[indexPath.row] {
do {
try realm.write {
realm.delete(item)
}
} catch {
print("Error deleting item, \(error)")
}
tableView.reloadData()
}
假设我在数据库中有一些数据,例如 1,2,3,4,5。当我删除“2”时,我希望它像 1、3、4、5 但它是 1、5、3、4。因此最后一个条目分别显示在底部的条目将始终替换已删除的数据。我该如何解决?
Objects is Realm are stored as unordered if you want them in order then you have to apply below code or store it as a List.
let result = realm.objects(ModelClass).sorted("attribute", ascending: true)
- 模型Class 是您的领域对象的名称Class
- 属性是 对象 属性 您想要它们的基础。
希望对您有所帮助。