如何将数据传递到下一个表视图?
所以我有一个存储密钥与多个值的字典。我有两个视图控制器,都包含一个表视图和自定义单元格。如何将数据传递到下一个表视图?
我已将键放入数组中,并将它们分配给第一个视图控制器中单元格标签的文本。当转换到下一个视图控制器时,我想传递按下的键的值。因此,例如,如果在第一个视图控制器中选择了包含字典中第一个键的行,则第一个键的所有值都将被分配给以下视图控制器的单元格标签文本,等等。这是可能的,如果是的话,那么实现这个的最好方法是什么?
这里是我的第一个视图控制器代码:
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}
}
下面是第一控制器塞格斯到第二视图控制器代码:
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
}
我建议在上面声明3个变量表视图1:
-
var trainingCategories: [String]?
拿你的字典键 -
var arrayForKey: [String]?
从字典 -
var selectedKey: String?
保持适当的数组来保存所选择的字典键
另外创建在表视图2中的变量来保存的dict阵列你会通过从表图。例如var arrayForKey2: [String]?
将以下内容放在表格视图1的viewDidLoad
方法中,并从CellForRowAt
中删除。这将停止每次在每个可重用单元上调用CellForRowAt
时调用它。呼唤你的self.performSegue...
方法之前selectedKey = trainingCategories[indexPath.row]
:
trainingCategories = Array(trainingDict.keys)
trainingCategories?.sort { return $0 < $1}
在didSelectRowAt
,获得所选择的字典键。
最后覆盖表视图1您prepareForSegue
方法,将信息传递给表视图2:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
secondTableView = segue.destination as! DrillsViewController
//Get array for selected dictionary key
arrayForKey = trainingDict[selectedKey]
//Pass to second table view
secondTableView.arrayForKey2 = arrayForKey
}
}
在cellForRowAt
表视图,然后你可以设置单元格的标签如前。例如。 cell.textLabel?.text = "\(arrayForKey2[indexPath.row])"
更新程序可以修复:
-
下仍然在表视图1.请删除的
cellForRowAt
存在。var trainingCategories = Array(trainingDict.keys) trainingCategories.sort { return $0 < $1}
在表视图1的
viewDidLoad
,在下面的行开始之前除去var
。
var trainingCategories = Array(trainingDict.keys)
(1)你没有真正解释那些视图控制器之间的关系。 (2)您没有字典变量来接收来自第一个视图控制器的数据。 (3)当您尝试将数据从一个视图控制器发送到另一个视图控制器时,您不会告诉结果。 –
那么我如何用我的代码? –