我发现了很多有关节和UITableViews的信息,但是,它们都不能应用于我的情况,这有点棘手。我已经奋斗了一段时间。任何帮助将不胜感激。
我有一个数据库,该数据库由Year-> Month-> Day对象组成,箭头从父类别到子类别显示。
我试图弄清楚如何按Year-> Month划分TableView,并且有一种添加浮动标题的方法。 我的意思是:当用户在2018年和6月在顶部滚动时,我可以在其下方看到2018年和6月,但是当用户进一步滚动至7月时,则在2018年在顶部,而7月在其下,依此类推。
到目前为止,我已经设法用按天对数据进行排序的TableView来填充数据,但是没有节:(
这是我的数据库结构:
年份:
class Year: Object {
@objc dynamic var id: Int = 0
@objc dynamic var number: Int = 2008
let months = List<Month>() //forward relationship to Months
override class func primaryKey() -> String {
return "id"
}
}
月份:
class Month: Object {
@objc dynamic var id: Int = 0
@objc dynamic var number: Int = 0
@objc dynamic var monthName: String = ""
@objc dynamic var monthYear: Int = 0
let days = List<Day>() //forward relationship to Days
let parentYear = LinkingObjects(fromType: Year.self, property: "months") //back relationship to year
override class func primaryKey() -> String {
return "id"
}
}
日期:
class Day: Object {
@objc dynamic var id: Int = 0
@objc dynamic var date: Date = Date()
@objc dynamic var number: Int = 0
@objc dynamic var dayName: String = ""
@objc dynamic var hoursPerDay: Double = 0.0
@objc dynamic var ratePerHour: Double = 0.0
@objc dynamic var minutesOfNormalPay: Double = 0.0
@objc dynamic var earnedPerDay: Double = 0.0
@objc dynamic var howManyRows: Int = 0
let items = List<Item>() //forward relationship to Items
let parentMonth = LinkingObjects(fromType: Month.self, property: "days") //back relationship to months
override class func primaryKey() -> String {
return "id"
}
}
这是我将数据加载回ViewController的方式,但是,在此阶段,我仅使用Days将数据加载到TableView中
private static let realm = try! Realm()
private var years: Results<Year> = {
let yearObjects = realm.objects(Year.self).sorted(byKeyPath: "number", ascending: true)
return yearObjects
}()
private var months: Results<Month> = {
let monthObjects = realm.objects(Month.self).sorted(byKeyPath: "number", ascending: true)
return monthObjects
}()
private var days: Results<Day> = {
let dayObjects = realm.objects(Day.self).sorted(byKeyPath: "date", ascending: true)
return dayObjects
}()
private var items: Results<Item> = {
let itemObjects = realm.objects(Item.self).sorted(byKeyPath: "id", ascending: true)
return itemObjects
}()
我尝试了几个可以在互联网上找到的示例,但到目前为止,没有一个可以奏效。