我在我的uitableview中使用2个ronem对象:
class SectionDate: Object {
@objc dynamic var date = Date()
let rowDates = List<RowDate>()
}
class RowDate: Object {
@objc dynamic var dateAndTime = Date()
}
tableViewData = realm.objects(SectionDate.self).sorted(byKeyPath: "date", ascending: isAscending)
func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewData[section].rowDates.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.rowDate = tableViewData[indexPath.section].rowDates[indexPath.row]
...
}
我将如何订购该部分。
看起来我可以作为部分的一部分做到这一点。
no,您无法为创建SectionDate
对象的rowDates
成员排序。List
是一种领域类型,不一定以分类方式存储列表。
您需要对对象的每个查询中的rowDates
对象进行排序。一个建议是将计算的属性添加到SectionDate
类(计算 - 未存储),该类别返回了根据需要排序的查询。然后在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中制作数据的静态副本,但是除非您遇到任何特定问题,否则我认为这是不需要的。