如何将数据传递到下一个表视图?

时间:2017-08-07 22:24:42

标签: swift uitableview dictionary segue datasource

所以我有一个包含多个值的存储键的字典。我有两个视图控制器,都包含一个表视图和自定义单元格。

我已将键放在一个数组中,并将它们指定为第一个视图控制器中单元格标签的文本。转换到下一个视图控制器时,我想传递按下的键的值。因此,例如,在第一个视图控制器中,如果您选择了包含字典中第一个键的行,则第一个键的所有值都将分配给以下视图控制器的单元格标签文本,依此类推。这是可能的,如果是这样的话,最好的方法是什么呢?

这是第一个视图控制器的代码:

 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

       }

1 个答案:

答案 0 :(得分:0)

我建议在表格视图1的顶部声明3个变量:

  1. var trainingCategories: [String]?来保存你的词典
  2. var arrayForKey: [String]?从字典中保存适当的数组
  3. var selectedKey: String?保留所选的dict键
  4. 还在表视图2中创建一个变量来保存将从表视图1传递的dict数组。 var arrayForKey2: [String]?

    将以下内容添加到表格视图1的viewDidLoad方法中,然后从CellForRowAt中删除。每次在每个可重用单元格上调用CellForRowAt时,都会停止调用它。

        trainingCategories = Array(trainingDict.keys)
        trainingCategories?.sort { return $0 < $1}
    

    didSelectRowAt中,在调用selectedKey = trainingCategories[indexPath.row]方法之前获取所选的词典键self.performSegue...

    最后覆盖表视图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
        }
    }
    

    在表格视图2的cellForRowAt中,您可以像以前一样设置单元格标签。例如。 cell.textLabel?.text = "\(arrayForKey2[indexPath.row])"

    更新修正:

    • 表视图的cellForRowAt中仍然存在以下内容:请删除。

      var trainingCategories = Array(trainingDict.keys)
      trainingCategories.sort { return $0 < $1}
      
    • 在表格视图1的viewDidLoad中,在以下行的开头删除var

    var trainingCategories = Array(trainingDict.keys)