我想填充一個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