gpt4 book ai didi

ios - fatal error : Index out of range tableview

转载 作者:行者123 更新时间:2023-11-28 14:59:52 26 4
gpt4 key购买 nike

谁能看出我错在哪里? tableView Controller 应显示 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' 是从前一个 View Controller 发送的,包含值 1 或 4。 “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

的情况下这将是一个错误

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

关于ios - fatal error : Index out of range tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49005472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com