swift - 如何使第二个表的 View 列表依赖于在前一个 TableView 中选择的行?

标签 swift dictionary tableview cell textlabel

我有一个字典,其中的键有多个值。在第一个 View Controller 上,我希望字典的键是 TableView 单元格的文本,每行一个。根据选择的行(具有字典键作为标签),我希望该键的值是以下 View Controller 上 TableView 单元格的文本。几乎就像一个嵌套的设置。因此,如果您选择带有特定键的一行作为其文本,下一个 View Controller 的 TableView 将在另一个 TableView 中显示其值。与 iPhone 上的设置选项卡类似,如果您选择的一行有一个显示其选项的标题,那么在下面的 View Controller 中,您将始终只能获得这些选项。实现这个的最好方法是什么,我已经尝试了很长时间但无济于事。现在,我当前的代码出现一个错误,指出索引超出范围。

这是第一个 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", "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.
}

第二 View Controller 代码:

 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

       }

最佳答案

你可以在performSegue中传递你想要的,例如indexpath 或您选择的

因为你的 TableView 的数据源是一个字典。您可以将其设为 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]
    }
}

关于swift - 如何使第二个表的 View 列表依赖于在前一个 TableView 中选择的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558815/

相关文章:

ios - Xcode 11.0-swift 的 Core Plot iOS 编译器错误

swift - 为什么iOS16的NavigationLink被禁用了?

python - 如何使用列表中的键和分隔空列表的值创建字典?

ios - 使用仪器自动化 ui 测试从 TableView 中删除项目

iOS UITableView 自定义 RowHeightt

swift - Admob adView DidReceive 广告在 UITableViewCell 中不起作用

ios - 如何添加动态部分但仍然只添加到其中一个部分?

ios - Swift:使表格 View 明显可滚动

python - 如何在字典中的 if 语句中实现正则表达式

java - 在 Thymeleaf 中将 map 渲染到表中的 EL 语法是什么?