【发布时间】:2016-07-29 01:03:20
【问题描述】:
我正在尝试使用两个 Firebase 对象数组(称为快照)填充包含 2 个部分的 tableView。当我尝试加载 tableView 时,我的 cellForRowAtIndexPath 函数出现错误:fatal error: Index out of range。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PersonCell", forIndexPath: indexPath) as! PersonCell
//set cell text
let guardianDict = guardians[indexPath.row].value as! [String : AnyObject] // error happens here
let dependentDict = dependents[indexPath.row].value as! [String : AnyObject]
cell.personName.text = "test"
return cell
}
这是我定义我的部分的方式:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch(section){
case 0: return "Dependents"
case 1: return "Guardians"
default: return ""
}
}
有什么想法吗? 谢谢!
编辑:添加 numberOfSections 和 numberOfRowsInSection:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
switch(section){
case 0: return self.dependents.count
case 1: return self.guardians.count
default: return 1
}
}
【问题讨论】:
-
您对
UITableViewDataSource实施的其余部分有什么要求?特别是numberOfRowsInSection和numberOfSections?这就是cellForRowAtIndexPath中的indexPath值的来源。此外,这些部分的索引为零,因此您必须使用case 0和case 1。 -
感谢您的回复!修复了零索引。添加了您要求的部分
标签: ios swift uitableview firebase firebase-realtime-database