Ale*_*cur 5 sorting core-data nsfetchedresultscontroller swift
我有具有 NSDate 属性的对象,我需要将它们分成两部分(第一部分 - 未来事件,第二部分 - 历史事件),然后对于第一部分,我需要按日期属性按升序对它们进行排序,第二部分按降序对它们进行排序命令。知道如何进行排序吗?
Wil*_*mer -2
首先在 fetchRequest 中设置排序描述符
func itemFetchRequest() -> NSFetchRequest{
let fetchRequest = NSFetchRequest(entityName: "Events")
let primarySortDescription = NSSortDescriptor(key: "futureEvents", ascending: true)
let secondarySortDescription = NSSortDescriptor(key: "historicEvents", ascending: false)
fetchRequest.sortDescriptors = [primarySortDescription, secondarySortDescription]
return fetchRequest
}
Run Code Online (Sandbox Code Playgroud)
然后设置你的部分数量
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
let numberOfSections = frc.sections?.count
return numberOfSections!
}
Run Code Online (Sandbox Code Playgroud)
最后是你的节标题
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
let sectionHeader = Future Events
let sectionHeader1 = Historic Events
if (section== "0") {
return sectionHeader
} else {
return sectionHeader1
}
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1366 次 |
| 最近记录: |