2016-07-29 4 views
0

2つのセクションを持つtableViewを、Firebaseオブジェクトの2つのアレイ(スナップショットと呼ばれます)を使用して作成しようとしています。私はtableFor:fatal error: Index out of rangeをロードしようとすると、私のcellForRowAtIndexPath関数にエラーが発生します。ここで致命的なエラー:2つのセクションを持つ範囲外のテーブルビュー

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 "" 
     } 
    } 

任意のアイデア? ありがとう!

EDIT: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'値がどこから来るかです。また、セクションはゼロインデックス化されるので、 'case 0'と' case 1'を使う必要があります。 – Deyton

+0

返信いただきありがとうございます!ゼロインデックス付けを修正しました。あなたが要求したセクションを追加しました – winston

答えて

2

あなたはさまざまなソースからの各セクションを使用して、テーブルに2つのセクションがあります。あなたは右のアレイにアクセスするためにあなたのcellForRowIndexPath機能にチェックを追加する必要があります。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] 
    } 
    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

あなたの二つの配列は、異なるサイズのものであってもよいです適切な配列。現在、この関数を呼び出すたびに両方の配列にアクセスしているため、一方の配列が他方の配列よりも小さいときにインデックスから範囲外の例外が発生します。

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 
} 
関連する問題