【问题标题】:Fatal Error: Index out of range tableview致命错误:索引超出范围表视图
【发布时间】:2018-08-06 21:51:55
【问题描述】:

谁能看到我哪里出错了? tableView 控制器应显示 1 或 4 个部分(取决于前一个视图控制器的输入)以及来自 array(s) 的动态行数。

该程序应该使用不同的数组填充每个部分,但是在 Switch 语句的第三种情况下,我收到了 Index out of range 错误。部分和行在viewdidLoad() 方法中设置,selectPic() 方法相应地更改houseImg 变量。

我正在使用 Swift 4

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if checkData == 0 {
            //Inserting one section and filling the cells with the array
            saveCount = pupilNames.count
                if saveCount > counter{
                    cell.imageView?.image = houseImg
                    cell.textLabel?.text = "Pupil Name:  \(pupilNames[indexPath.row])"
                    cell.detailTextLabel?.text = "Score:  \(pupilScores[indexPath.row])"
                    counter = counter+1
                }
            checkData = 2
            return cell
        }
        else if checkData == 1 {
            //Inserting four sections and populate 4 arrays for each section
            var countIf: Int = 0
            var secondCount: Int = 0
            houseCount = 4
                if checkCount > houseCont {
                    switch houseCont {
                        case 0:
                            chosenHouse = 1
                            selectPic()
                            countIf = pupilNames.count
                            repeat {
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(pupilNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(pupilScores[indexPath.row])"
                                secondCount = secondCount+1
                            } while countIf > secondCount
                        case 1:
                            chosenHouse = 2
                            selectPic()
                            countIf = secondNames.count
                            secondCount = 0
                            repeat {
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(secondNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(secondScores[indexPath.row])"
                                secondCount = secondCount+1
                            } while countIf > secondCount
                        case 2:
                            chosenHouse = 3
                            selectPic()
                            countIf = thirdNames.count
                            secondCount = 0
                            repeat {
                                cell.imageView?.image = houseImg
                                //*******Error occurring on Pupil Name line *********
                                cell.textLabel?.text = "Pupil Name:  \(thirdNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(thirdScores[indexPath.row])"
                                secondCount = secondCount+1
                            } while countIf > secondCount
                        case 3:
                            chosenHouse = 4
                            selectPic()
                            repeat {
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(fourNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(fourScores[indexPath.row])"
                                secondCount = secondCount+1
                            }while countIf > secondCount
                    default:
                            houseCont = 5
                            checkData = 2
                    }
                    houseCont = houseCont+1
                }
        }
    return cell
}

编辑: 'houseCount' 是从前一个视图控制器发送的,包含一个或四个值。 'saveCount' 包含一个通过将所有数组的计数相加而创建的数字,这是在填充数组的方法中完成的。

saveCount = pupilNames.count+secondNames.count+thirdNames.count+fourNames.count

override func numberOfSections(in tableView: UITableView) -> Int{
    return houseCount
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return saveCount
}

最终编辑:感谢大家的帮助,这两个答案的混合帮助我解决了我的错误。这是我解决问题的最终代码,我删除了 if 语句和重复循环,并更改了一些代码以解决我的问题。

else if checkData == 1 {
            houseCount = 4
                    switch indexPath.section {
                        case 0:
                            chosenHouse = 1
                            selectPic()
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(pupilNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(pupilScores[indexPath.row])"
                        case 1:
                            chosenHouse = 2
                            selectPic()
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(secondNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(secondScores[indexPath.row])"
                        case 2:
                            chosenHouse = 3
                            selectPic()
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(thirdNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(thirdScores[indexPath.row])"
                        case 3:
                            chosenHouse = 4
                            selectPic()
                            //saveCount = fourNames.count
                                cell.imageView?.image = houseImg
                                cell.textLabel?.text = "Pupil Name:  \(fourNames[indexPath.row])"
                                cell.detailTextLabel?.text = "Score:   \(fourScores[indexPath.row])"
                    default:
                            houseCont = 5
                            checkData = 2
                    }
                    houseCont = houseCont+1
        }

【问题讨论】:

  • 你能告诉我们你的numberOfRows & numberOfSection 方法实现吗?
  • 那些repeat 循环看起来非常错误和可疑.. 为什么你重写文本 n 次而不是简单地设置最后一个值,你想要在那里的那个?此外,您在谈论部分,但现在您似乎完全忽略了indexPath.section

标签: ios swift uitableview tableview


【解决方案1】:

看看下面的代码,有几个警钟正在响起:

                       chosenHouse = 3
                       selectPic()
                       countIf = thirdNames.count
                       secondCount = 0
                       repeat {
                            cell.imageView?.image = houseImg
                            //*******Error occurring on Pupil Name line *********
                            cell.textLabel?.text = "Pupil Name:  \(thirdNames[indexPath.row])"
                            cell.detailTextLabel?.text = "Score:   \(thirdScores[indexPath.row])"
                            secondCount = secondCount+1
                        } while countIf > secondCount

首先使用repeat。里面的代码总是至少执行一次。我认为如果thirdNames.count == 0

然而,更奇怪的是,重复循环的内容在迭代之间并没有改变。你不会做任何取决于唯一改变的事情 - secondCount。如果countIf 是 10,您只需将相同的名称和分数分配给相同的两个控件 10 次。

【讨论】:

  • thirdNames.count 永远不能等于 0,否则显示 Table View Controller 的按钮将不会被启用。这是否意味着我需要增加数组或索引路径?
  • "这是否意味着我需要增加数组或索引路径?"我希望您在每次迭代中使用不同的单元格和不同的数据,否则有什么意义?
【解决方案2】:

我不确定,但根据我在最后一个 switch 语句中的说法,当它执行时,插入部分时会出现问题,因为它可能会以某种方式超过索引路径行数。编写代码的方式也没有考虑到 indexPath.section 。

【讨论】:

  • 我是 swift 新手,还在学习语法,我可以在我的 switch 语句中实现它吗?
  • 你可以尝试使用 " override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} 来定义特定部分的行数。然后你可以使用 func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {} 在其中你可以使用 switch indexPath.section 切换部分,并为每个部分做相应的编码。
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多