gpt4 book ai didi

ios - 如何将一个 UITableView 链接到另一个 UITableView

转载 作者:可可西里 更新时间:2023-11-01 02:19:32 26 4
gpt4 key购买 nike

我目前正在使用 swift 制作一个包含汽车信息的应用程序。我正在使用 UITableView 来显示品牌、型号、年份。

我想知道的是,是否可以根据用户输入将一个 UITableView 链接到另一个 UITableView,例如:

表格 View 1(制作)

  • 奥迪
  • 本田

表格 View 2(模型)

  • 奥迪 -> A1, A2, A3........
  • 本田 -> 思域、爵士...

表格 View 3(年)

  • 奥迪 -> A3 -> 2005,2006,2007.....
  • 本田 -> 思域 -> 2005,2006,2007.....

Code for tableview 1

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.Makes = [Make(name: "Audi"),Make(name: "Nissan"),Make(name: "Fiat"),Make(name: "Ford"),Make(name: "Honda"),Make(name: "Mercedes-Benz"),Make(name: "Lexus"),Make(name: "BMW"),Make(name: "Vauxhall"),Make(name: "VW")]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Makes.count

}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
var make = Makes[indexPath.row]
cell.textLabel?.text = make.name
return cell
}

最佳答案

1.- 以良好的方式组织您的数据,可以是一张图表、一棵树,或者只是将您的所有数据相关联的列表。

2.- 为简单起见,制作函数将为您提供每个 TableView 的相应数据。可以说:

func getModels(make: Makes) -> [Model]
func getYears(model: Model) -> [Years]

或者只是

func getModels(make: String) -> [String]
func getYears(model: String) -> [String]

还有一些辅助函数,可以让您在后面实现任何数据结构,例如:

func getMaker(int:Int) -> Maker?func getMaker(int: Int) -> String?

3.- 你必须记住你选择了哪些可能的制造商和型号,现在,保持这样:

var selectedMaker: String?
var selectedModel: String?

4.- 我假设您的所有 UITableViews 都在同一个 UIViewControllerUITableViewController 中,因此您需要决定相应的数据向每一个人展示。为此,您需要区分每一个,如何显示,带有标签,实例相等性等。我建议以后的可读性和使用便利性最终拥有一个返回数字的函数?也许,对应于tableview。为了便于解释,我们称它为 func whichTableIsThis(tableView: UITableView) -> Int?

5.- 你的代表应该为每个表格 View 工作不同。在这里,我们将使用我们全新的函数,如果此 TableView 不是其中之一,则该函数必须返回 1、2 或 3 ..nil。 :)

extension YourViewControlerWithTableViews: UITableViewDelegate, UITableViewDataSource {

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//I'm assuming you will have only one cell, lets call it `AttributesTableViewCell`
var cell = tableView.dequeueReusableCellWithIdentifier("yourCellName", forIndexPath: indexPath) as! AttributesTableViewCell
cell.attributeValue.text = ""
if let tableNumber = whichTableIsThis(tableView) {
//here you will be checking for every of your tree cases, for this example I will check just for Models
//OK, so tableNumber returned 2
if tableNumber == 2 && selectedMaker != nil{
let value = getModels(selectedMaker!)[indexPath.row]
cell.attributeValue.text = value
}
//...
}
return cell
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let tableNumber = whichTableIsThis(tableView) {
//here you will be checking for every of your tree cases, for this example I will check just for Models
//OK, so tableNumber returned 2
if tableNumber == 2 && selectedMaker != nil{
return getModels(selectedMaker!).count
}
//...
}
return 0
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let tableNumber = whichTableIsThis(tableView) {
//here you will be checking for every of your tree cases, for this example I will check just for Maker
//OK, so tableNumber returned 1
if tableNumber == 1 {
selectedMaker = getMaker(indexPath.row)
//Here you must refresh data for your next tables in hierarchy, to allow them to refresh with new data
selectedModel = nil
selectedYear = nil
tableview2.reloadData()
tableview3.reloadData()
}
//...
}
}
}

然后..应该就这些了。希望对您有所帮助!

关于ios - 如何将一个 UITableView 链接到另一个 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com