我在UITableView中使用2个Realm对象作为数据源:
class SectionDate:Object {
@objc dynamic var date = Date() 让rowDates = List< RowDate>()}class RowDate:Object {
…
不,你不能排序 rowDates 关于创建的成员 SectionDate 宾语。该 List 是一种不必(必然)以排序方式存储列表的Realm类型。
rowDates
SectionDate
List
你需要对它进行排序 rowDates 对象的每个查询上的对象。一个建议是将计算属性添加到 SectionDate 返回按要求排序的查询的类(计算 - 未存储)。然后访问该属性 cellForRowAt 功能。例如。:
cellForRowAt
extension SectionDates { var sortedRowDates { return rowDates.sorted(byKeyPath: "date", ascending: isAscending) } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ... cell.rowDate = tableViewData[indexPath.section].sortedRowDates[indexPath.row] ... }
这当然意味着正在为每个单元格运行查询,但这没关系。还有其他解决方案,例如在viewDidLoad中制作数据的静态副本,但我认为除非遇到任何特定问题,否则不需要这样做。