2017-08-08 58 views
0

我有兩個視圖控制器既包含表意見,我希望它在那裏,一旦行選擇在第一個表視圖,當它塞格斯到下表中查看電池的文本的對應與他們在第一個表選擇任何行視圖。想象一下設置應用程序,一旦你在行上選擇,在下一個視圖控制器中,總是顯示與之前表格視圖中選擇的內容相對應的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 

     } 
+0

在你的代碼的行你得到這個例外? – PGDev

回答

0

值不被傳遞也許是因爲要執行賽格瑞第一

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

更換這樣

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

請嘗試,讓我知道如果問題仍然存在

+0

是仍然收到錯誤 –

+0

在控制檯中打印什麼錯誤? –

+0

'致命錯誤:索引超出範圍' –

1

在下面的一行:

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

您正在訪問陣列trainingCategoriesindexPath.row元素。

首先檢查trainingCategories是否包含了許多內容。

訪問索引的陣列超過它包含將給出一個運行時異常"Array index out of bounds"的元素。

+0

如果有任何答案有助於解決您的問題,請將其標記爲已接受,以便幫助其他人知道正確的答案。 – PGDev

相關問題