【发布时间】: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