gpt4 book ai didi

swift - 为什么在使用 TableView 委托(delegate)时会出现索引超出范围的 fatal error ?

转载 作者:行者123 更新时间:2023-11-28 15:23:50 26 4
gpt4 key购买 nike

我有两个 View Controller ,它们都包含表格 View ,我希望它在第一个表格 View 上选择一行后,当它转到下一个表格 View 时,单元格文本对应于他们在第一个表格中选择的任何行 TableView 。想象一下设置应用程序,一旦你选择行,在下一个 View Controller 中,你总是会看到与你在前一个 TableView 中选择的内容相对应的相同单元格文本选项。我正在使用字典的键和值,并匹配它们在 did select at 行中的索引,但是当我在运行时运行代码时,我收到一个错误,指出索引超出范围,有人知道为什么会这样吗?

代码到发生错误的第一个 View Controller :

 import UIKit

var trainingDict = ["Ball Handling" : ["1 Ball Stationary Drills", "1 Ball Combo Moves", "2 Ball Stationary Drills", "2 Ball Combo Moves", "2 Ball Partner Drills", "Handle Hoop Drills", "Placeholder"], "Shooting" : ["Form Shooting", "Spot Shooting", "Off The Dribble Shots", "Pull Up Jumpshots", "Catch & Shoots", "Free Throws", "Partner Shooting"], "Defense" : ["5 Star Drill", "Full Court Def. Slides", "1 v 1 Closeouts", "Gauntlet Drill", "Tennis Ball Reaction Drill", "Lane Slides", "Place Holder"], "Advanced Drills" : ["D Man Series", "Iso Series", "Double Move Series", "Gauntlet Series", "John Wall Drill", "Floater Series", "PlaceHolder"], "Vertimax Drills" : ["One Foot Jumps", "Box Jumps", "Resitance Slides", "Resistance Jumps", "Resistance Ball Handling", "Vertimax Sprints", "Slam Drill"], "Full Workouts" : ["Workout A", "Workout B", "Workout C", "Workout D", "Workout E", "Workout F", "Workout G"], "BHB Products" : ["Handle Hoops", "Handle Cubes", "Strech Bands", "Advocare", "Placeholder", "Placeholder2", "Placeholder3"]]
var gradient : CAGradientLayer!
var myIndex = 0


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {



@IBOutlet weak var tableView: TableView!

var trainingCategories = [String]()
var arrayForKey = Array(trainingDict.values)
var selectedKey = 0




public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

return trainingDict.count
}




public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "bell" , for: indexPath) as! ViewControllerTableViewCell

//cell details
cell.backgroundColor = UIColor.clear

//gradient details
gradient = CAGradientLayer()
gradient.frame = tableView.bounds
gradient.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor]
tableView.layer.insertSublayer(gradient, at: 0)
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

//details what the text label of cell displays
var trainingCategories = Array(trainingDict.keys)
trainingCategories.sort { return $0 < $1}

cell.textLabel?.text = trainingCategories[indexPath.row]
cell.textLabel?.textColor = UIColor.white


print(trainingCategories.count)

return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

performSegue(withIdentifier: "segue", sender: self)
selectedKey = Int(trainingCategories[indexPath.row])!
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
if let secondTableView = segue.destination as? DrillsViewController {

secondTableView.keyIndex = selectedKey

secondTableView.arrayForKey2 = arrayForKey

}
}

}




override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
var trainingCategories = Array(trainingDict.keys)
trainingCategories.sort { return $0 < $1}
}

为第二个 View Controller 添加代码:

  import UIKit



class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var arrayForKey2 = [[String]]()
var keyIndex = Int()

@IBOutlet weak var tableView: DrillsTableView!

@IBOutlet weak var drillLabel: UILabel!


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

return arrayForKey2.count
}

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for: indexPath) as! DrillsTableViewCell

//clear background color needed in order to display gradient cell
cell.backgroundColor = UIColor.clear

//gradient configuration
gradient = CAGradientLayer()
gradient.frame = tableView.bounds
gradient.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor]
tableView.layer.insertSublayer(gradient, at: 0)
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)



//attributes for watch/play button
cell.playButton.layer.shadowColor = UIColor.yellow.cgColor
cell.playButton.layer.shadowOffset = CGSize(width: 2, height: 2)
cell.playButton.layer.shadowOpacity = 0.7
cell.playButton.layer.shadowRadius = 1

//details for cell label display

cell.drillTitle.text = "\(arrayForKey2[keyIndex][indexPath.row])"


return cell
}




override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self

}

最佳答案

在下面一行:

selectedKey = Int(trainingCategories[indexPath.row])!

您正在访问数组 trainingCategoriesindexPath.row 元素。

首先检查 trainingCategories 是否包含那么多元素。

访问一个数组的索引大于它包含的元素会产生运行时异常“Array index out of bounds”

关于swift - 为什么在使用 TableView 委托(delegate)时会出现索引超出范围的 fatal error ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559690/

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