Also habe ich ein Wörterbuch der gespeicherten Schlüssel mit mehreren Werten. Ich habe zwei View-Controller, beide enthalten eine Tabellenansicht und benutzerdefinierte Zellen.Wie übergebe ich Daten zur nächsten Tabellenansicht?
Ich habe die Schlüssel in einem Array platziert und ihnen zugewiesen, um den Text der Zellbezeichnung im ersten Ansichtscontroller zu sein. Beim Übergang zum nächsten View-Controller möchte ich die Werte des gedrückten Keys weitergeben. Wenn Sie beispielsweise im ersten View-Controller eine Zeile ausgewählt haben, die den ersten Schlüssel im Wörterbuch enthält, werden alle Werte des ersten Schlüssels dem Text des Zellenlabels für den folgenden View-Controller usw. zugewiesen. Ist dies möglich, und wenn ja, wie könnte dies am besten umgesetzt werden?
Hier ist mein Code für den ersten 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"], "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"], "Advanced Drills" : ["D Man Series", "Iso Series", "Double Move Series", "Gauntlet Series", "John Wall Drill", "Floater Series"], "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", "Workuot E", "Workout F", "Workout G"], "BHB Products" : ["Handle Hoops", "Handle Cubes", "Strech Bands", "Advocare"]]
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}
}
Hier Code für die zweite View-Controller, dass der erste Controller segues:
import UIKit
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
}
(1) Sie erklären nicht wirklich um die Beziehung zwischen diesen View-Controller. (2) Sie haben keine Wörterbuchvariable, um die Daten vom ersten View-Controller zu erhalten. (3) Sie sagen das Ergebnis nicht, wenn Sie versuchen, Daten von einem Ansichtscontroller zu einem anderen zu senden. –
dann wie mache ich das so mit meinem Code? –