【问题标题】:fatal error: index out of range tableview with two sections致命错误:索引超出范围的 tableview 有两个部分
【发布时间】:2016-07-29 01:03:20
【问题描述】:

我正在尝试使用两个 Firebase 对象数组(称为快照)填充包含 2 个部分的 tableView。当我尝试加载 tableView 时,我的 cellForRowAtIndexPath 函数出现错误:fatal error: Index out of range

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 实施的其余部分有什么要求?特别是numberOfRowsInSectionnumberOfSections?这就是cellForRowAtIndexPath 中的indexPath 值的来源。此外,这些部分的索引为零,因此您必须使用 case 0case 1
  • 感谢您的回复!修复了零索引。添加了您要求的部分

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:

您的表格中有两个部分,每个部分来自不同的来源。您需要在 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
}

【讨论】:

  • 成功了!这种逻辑完全有道理。这是我第一次尝试将来自多个源的数据加载到多个部分。我不知道检查这样的部分是如此容易!再次感谢
【解决方案2】:

您的两个数组可能具有不同的大小,因此在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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    相关资源
    最近更新 更多