我正在尝试在 Swift 中实现一个 iOS TableView ,它执行以下操作:
- 通过计算当前存储在我的 Core Data 实体中的对象数量生成多个部分
- 为每个部分创建一行,每一行代表/保存我的核心数据实体中的一个对象
请原谅零碎的代码,它需要整理。
这是我设置节数的函数。它只是返回获取的结果数并将其作为我的部分数提供。太好了。
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
let count = self.fetchedResultsController.fetchedObjects?.count
if (count != 0){
return count! }
else {
return 0
}
}
但是我不知道如何调整项目文件中包含的基本 numberOfRowsInSection 方法。对于超出 0 范围的数组,它会在 NSRangeException 上跳闸。我猜这是 [section] 数组。
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
简单地返回 1,对于我想要的每个部分的一行,这是行不通的,落在上面的相同错误上。
能不能请一些编码员给我指明正确的方向。
谢谢!
编辑:每当我返回任何大于 1 的 numberOfSectionsInTableView 时,这里就会发生错误。
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
最佳答案
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.fetchedResultsController.sections!.count
}
此处返回的是节数,而不是行数。
关于ios - Swift:动态填充 numberOfSectionsInTableView 和 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349707/