我想填充一个tableView与2节使用两个Firebase对象(称为快照)数组。当我尝试加载tableView时,在我的cellForRowAtIndexPath函数中出现错误:fatal error: Index out of range
。致命错误:索引超出范围tableview与两个部分
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'值的来源。此外,这些部分将被零索引,所以你将不得不使用'情况0'和'情况1'。 – Deyton
感谢您的回复!修复了零索引。添加了您请求的部分 – winston