为什么使用表格视图委托时会出现索引超出范围的致命错误?
我有两个视图控制器既包含表意见,我希望它在那里,一旦行选择在第一个表视图,当它塞格斯到下表中查看电池的文本的对应与他们在第一个表选择任何行视图。想象一下设置应用程序,一旦你在行上选择,在下一个视图控制器中,总是显示与之前表格视图中选择的内容相对应的SAME单元格文本选项。我使用的是字典的键和值和匹配其在指数排没选择,但是当我在运行时运行代码,我得到的是说指数超出范围的错误,谁知道为什么会这样?为什么使用表格视图委托时会出现索引超出范围的致命错误?
代码到第一视图控制器,其中错误正在发生的事情:
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}
}
添加的代码为第二视图控制器:
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
}
值不被传递也许是因为要执行赛格瑞第一
performSegue(withIdentifier: "segue", sender: self)
selectedKey = Int(trainingCategories[indexPath.row])!
更换这样
selectedKey = Int(trainingCategories[indexPath.row])!
performSegue(withIdentifier: "segue", sender: self)
请尝试,让我知道如果问题仍然存在
是仍然收到错误 –
在控制台中打印什么错误? –
'致命错误:索引超出范围' –
在下面的一行:
selectedKey = Int(trainingCategories[indexPath.row])!
您正在访问阵列trainingCategories
的indexPath.row
元素。
首先检查trainingCategories
是否包含了许多内容。
访问索引的阵列超过它包含将给出一个运行时异常"Array index out of bounds"
的元素。
如果有任何答案有助于解决您的问题,请将其标记为已接受,以便帮助其他人知道正确的答案。 – PGDev
在你的代码的行你得到这个例外? – PGDev