私は2つのビューコントローラを持っていますが、テーブルビューを含んでいて、最初のテーブルビューで行が選択されたとき、次のテーブルビューと重なるときに最初のテーブルで選択された行ビュー。設定アプリのように、行を選択すると、次のビューコントローラーでは、前のテーブルビューで選択した内容に対応する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}
}
は、第2のビューコントローラのコードを追加:最初セグエ実行されるので、値は多分渡されていない
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
}
あなたのコードのどの部分でこの例外が発生していますか? – PGDev