主要思想是将所有节放在一个数组中,或者您可以建议其他解决方案来构建表。我在下面有以下代码来为表准备数据:
enum ExerciseSection {
case empty
case exerciseGroup(group: ExerciseGroup)
}
struct ExerciseGroup {
var muscleGroupName: String
var exercises: [ExerciseEntity]
var selectedExercisesIndexes: [Int]
}
如您所见,使用此ExerciseSection
枚举,我可以简单地检查该部分是否为静态空白或它应该显示一些肌肉组名称。组也包含exercises
。因此,我可以简化构建所需的单元格。
因此,我通过创建ExerciseSection
数组为表准备数据。
在此音乐会示例中,我的空白单元格是一个将我重定向到另一个屏幕的单元格。
看起来像这样:
[empty cell for section 0, group for section 1, group for section 2... and etc]
现在,我改变了准备自己的部分的想法,而是开始使用CoreStore.monitorSectionedList
typealias ListEntityType = ExerciseEntity
let monitor = CoreStore.monitorSectionedList(
From<ListEntityType>()
.sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
"\(String(describing: sectionName)) years old"
}
.orderBy(.ascending(\.name))
)
因此,现在我的数据通过关系muscle name
自动分组。
我可以简单地访问Monitor的实例,并查看它有多少部分以及相应部分有多少行。太棒了!
但是我现在的问题是,如何将具有关于分组对象和有关分组的所有信息的监视对象与我的静态单元格结合起来。
在上面的示例中,我有第0部分的firs元素为空单元格,但监视器也已经有第0部分。
所以我需要添加一个1 +
,我真的不喜欢它,因为这是一个神奇的数字,总有一天它会让我感到惊讶。
func numberOfSections(in tableView: UITableView) -> Int {
return 1 + (monitor.numberOfSections() ?? 0) // My static section + monitor sections
}
上次我只拥有所有节[ExerciseSection]
的数组,因此无需通过1 +
控制代码
我需要以某种方式粘贴我的静态部分信息和monitor.sections
您以前可能从未使用过CoreStore
,所以没关系,您可以将monitor
对象想像为一个对象,该对象具有一些用于表示节的组,而这些组具有用于表示行的项。所以我只需要结合它。
就我而言,您可以简单地看到静态单元格是列表中的第一项,但是我在寻找灵活的解决方案,我什至无法想象如何在列表的中间显示静态单元格。
作为解决方案,我可以遍历监视器对象并从中创建枚举。不确定。
答案 0 :(得分:0)
嗯...“最简单”的方法是拥有计算属性sections
或类似属性。
类似的东西
var sections: [ExerciseSection] {
return [.empty] + monitor.sections
}
但是,如果监视器没有直接获取节的方法,那么最好的方法可能就是简单地列出“预节”。
let presections: [ExerciseSection] = [.empty]
func numberOfSections(in tableView: UITableView) -> Int {
return presections.count + (monitor.numberOfSections() ?? 0) // My static section + monitor sections
}
您可以添加一些功能来帮助您,例如
func section(at indexPath: IndexPath) -> ExerciseSection {
guard indexPath.section >= presections.count else {
return presections[indexPath.section]
}
return monitor.section(at: indexPath.section - presections.count)
}
您确实提到了遍历监视器对象,这对于较小的数据集可能会很好。缺点是您突然将数据存储在内存中。我不知道显示器的工作原理。
我已经使用Realm完成了此操作,但只存储了id以及每行的一些简单数据。