2017-08-08 1 views
1

J'ai un dictionnaire avec des clés qui ont plusieurs valeurs. Sur le premier contrôleur de vue, je veux que les clés du dictionnaire soient le texte de la cellule de vue de table, un pour chaque rangée. Selon la ligne (qui a une clé de dictionnaire en tant qu'étiquette) est sélectionnée, je veux que les valeurs de cette clé soient le texte de la cellule de vue de table sur le contrôleur de vue suivant. Presque comme une installation imbriquée. Donc, si vous choisissez une ligne avec une clé spécifique comme texte, la vue de table du contrôleur de vue suivant affichera ses valeurs dans une autre vue de table. Similaire à l'onglet des paramètres sur l'iPhone, si vous sélectionnez une ligne qui a un titre qui affiche ses options, dans le contrôleur de vue suivant, vous aurez toujours ces options. Quelle est la meilleure façon de mettre en œuvre cela, j'ai essayé pendant une longue période sans succès. En ce moment, mon code actuel reçoit une erreur indiquant que l'index est hors limites.Comment puis-je faire en sorte que la liste de vues d'une deuxième table dépende de la ligne sélectionnée dans la vue de table précédente?

Code est ici pour la première contrôleur de vue:

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 = [String]() 
var selectedKey = String() 




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.tag = indexPath.row 




    //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 

    return cell 
} 


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    myIndex = indexPath.row 
    selectedKey = trainingCategories[indexPath.row] 
    performSegue(withIdentifier: "segue", sender: self) 

} 

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

      //get array for selected key 
      arrayForKey = trainingDict[selectedKey]! 

      //pass to second table view 
      secondTableView.arrayForKey2 = arrayForKey 
      } 
     } 


    } 



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

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

Deuxième vue Code commande:

import UIKit 



class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var arrayForKey2 = [String]() 

@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[indexPath.row])" 


    return cell 
} 




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

     } 

Répondre

0

Vous pouvez passer ce que vous voulez dans performSegue, par exemple, le indexpath ou key vous sélectionnez. Puisque votre source de données de la vue de table est Dictionary

Vous pouvez en faire un Array pour conserver l'index. (Juste comme ce que vous avez fait dans cellForRowAt)

// didSelectRowAt 
let key = Array(trainingDict.keys)[indexPath.row] 
performSegue(withIdentifier: "segue", sender: key) 

// navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "segue" { 
     guard let secondTableView = segue.destination as? DrillsViewController, let key = sender as? String else { return } 

     secondTableView.arrayForKey2 = trainingDict[key] 
    } 
} 
+0

Cela m'a donné une erreur. Dit une référence ambiguë à l'indice –