我正在构建一个非常简单的时间表 iOS 应用程序来测试作为后端的 Swift 和 Parse.com。
我的 Parse 数据库中有一个名为“Class”的对象类型(用于学校类(class))。这些是包含 name
和 dayOfWeek
(周一至周五)的简单对象。
我想显示这些类似于 iOS 日历应用程序的“ ListView ”,每天都有一部分:星期一、星期二、星期三、星期四和星期五(不是像日历应用程序那样的实际日期,只是星期几的名称) .
然后我想在每一天下显示,只有从与相关部分具有相同 dayOfWeek
的查询返回的 PFObjects。
我尝试使用 PFQueryTableViewController 实现这一点,但遇到了奇怪的问题,没有部分名称和整个表格中的随机空单元格。
我认为我需要采用以下方法,但非常感谢您的帮助。
- 检索我所有
Class
对象的PFQuery
- 将
numberOfSectionsInTable
设置为 7(每天一个) - 将
titleForSection
设置为适当的名称 - 在
cellForRowAtIndexPath
中,循环返回的 Class 对象,根据dayOfWeek
将每个对象分配到正确的部分。 不确定该怎么做。
我四处搜索,发现似乎没有明确的教程或文档说明如何在多个部分显示 PFQuery 中的 PFObjects,并按对象的值之一排序。
如有任何帮助,我们将不胜感激。我相信这也会帮助很多其他人。
最佳答案
您可以使用包含当前部分名称的二维数组和一个用于保存项目的数组来创建动态部分。
然后您只需调用
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return sectionItems.items[section].count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return sectionItems.items[section].count
}
可以使用此类将字符串和数组作为参数来创建 sectionItems
class SectionItems:NSObject{
var sections:[String] = []
var items:[[String]] = []
func addSection(section: String, item:[String]){
sections = sections + [section]
items = items + [item]
}
}
您可以使用以下方式调用该函数
self.sectionItems.addSection("Intro", item: self.intro)
关于ios - 带有 PFObjects 的多个部分 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30857483/