如何根据前一个表视图中选择的行创建第二个表的视图列表?

时间:2017-08-08 03:29:22

标签: swift dictionary tableview cell textlabel

我有一个包含多个值的字典。在第一个视图控制器上,我希望字典的键是表视图单元格的文本,每行一个。根据选择哪一行(具有字典键作为标签),我希望该键的值是以下视图控制器上的表视图单元格的文本。几乎像嵌套设置。因此,如果您选择一行并使用特定键作为其文本,则下一个视图控制器的表视图将在另一个表视图中显示其值。与iPhone上的设置选项卡类似,如果您选择一个具有一个显示其选项的标题的行,则在以下视图控制器中,您将始终只获取这些选项。实现这一目标的最佳方法是什么,我已经尝试了很长时间但没有用。现在我的当前代码出现一个错误,指出索引超出范围。

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

  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.
}

}

第二个视图控制器代码:

 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)

您可以在performSegue中传递您想要的内容,例如indexpath  或者您选择key

由于表视图的数据源是Dictionary。您可以将其设为Array以保留索引。 (就像你在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]
    }
}
相关问题