致命错误:索引超出范围tableview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了致命错误:索引超出范围tableview相关的知识,希望对你有一定的参考价值。

任何人都可以看到我错在哪里? tableView控制器应显示1或4个部分(取决于前一个View Controller的输入)以及来自阵列的动态行数。

该程序应该用不同的数组填充每个部分,但是我在Switch语句的第三种情况下收到索引超出范围错误。截面和行在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
        }
答案

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

                       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次。

另一答案

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

以上是关于致命错误:索引超出范围tableview的主要内容,如果未能解决你的问题,请参考以下文章

致命错误:TableView 中的数组索引超出范围

致命错误:索引超出范围tableview

致命错误:索引超出范围

(Swift) 致命错误:索引超出范围

我收到错误:致命错误:索引超出范围

重新加载 ViewController 中的 tableview 让我在索引路径的 cellforRow 中出现“致命错误:索引超出范围”