2016-07-29 54 views
0

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

你有什麼其他'UITableViewDataSource'實現?特別是'numberOfRowsInSection'和'numberOfSections'?這就是'cellForRowAtIndexPath'中'indexPath'值的來源。此外,這些部分將被零索引,所以你將不得不使用'情況0'和'情況1'。 – Deyton

+0

感謝您的回覆!修復了零索引。添加了您請求的部分 – winston

回答

2

你在你的表兩個部分,每個部分來自不同來源的。您需要添加在你cellForRowIndexPath功能檢查,以訪問正確的數組:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("PersonCell", forIndexPath: indexPath) as! PersonCell 

    if indexPath.section == 0 
    { 
    let dependentDict = dependents[indexPath.row].value as! [String : AnyObject] 
    } 
    else if indexPath.section == 1 
    { 
    let guardianDict = guardians[indexPath.row].value as! [String : AnyObject] // error happens here 
    } 
    cell.personName.text = "test" 

    return cell 
} 
+1

工作正常!這個邏輯非常有意義。這是我第一次嘗試從多個來源加載數據到多個部分。我不知道檢查這樣的部分是如此容易!再次感謝 – winston

+0

沒問題。樂於幫助。 –

0

你的兩個數組可以是不同的大小,所以在cellForRowAtIndexPath你需要檢查哪一段你是返回一個小區只能訪問適當的數組。目前,您對這個函數的每個調用都訪問兩個數組,當其中一個數組比另一個小時,會導致索引超出範圍。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("PersonCell", forIndexPath: indexPath) as! PersonCell 

    if indexPath.section == 0 { 
     let dependentDict = dependents[indexPath.row].value as! [String : AnyObject] 
     cell.personName.text = dependentDict["name"] as! String //Or whatever element in the dictionary is needed 
    } else { 
     let guardianDict = guardians[indexPath.row].value as! [String : AnyObject] 
     cell.personName.text = guardianDict["name"] as! String //Or whatever element in the dictionary is needed 
    } 

    return cell 
} 
相關問題