2016-07-29 55 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 
} 
相关问题