我有三个视图控制器,每个都有一个表视图。在第一个VC的表格中,各大洲的名称,在选择任何大陆名称时,用户被带到VC,其中的表格视图已由来自所选大陆的国家/地区的名称填充。选择任何国家/地区的名称需要一到三个具有表格视图的VC,该表格由所述国家/地区的省份填充,在这种情况下,所选国家/地区的零碎名称作为省份,例如,如果选择德国,则选择德国它是:{" Ger"," ma"," ny"}。第一个和第二个tableviews工作正常,但第三个只能正确填充数据当用户从第一个表格视图(欧洲)的第一个元素进行跟踪时,如下图所示:
在第一个表视图(例如北美)上选择任何其他元素时,第二个表视图会被数据正确填充,但第三个表视图每次都被数据填充,就好像我选择了第一个表格视图所选择的元素一样第一个tableview(欧洲)上的元素与这些图片一样:,
这是我负责第一个表格视图的代码:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var TableView: UITableView!
var continentData = ["Europe", "Asia", "Africa", "North America", "South America", "Australia"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return continentData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "continentCell", for: indexPath)
cell.textLabel?.text = continentData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let countryVC = CountryVC()
countryVC.customInit(continentIndex: indexPath.row, title: continentData[indexPath.row])
self.navigationController?.pushViewController(countryVC, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
}
负责第二个tableView的代码:
import UIKit
class CountryVC: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let countryData = [
["Germany", "France", "Italy", "Spain","Sweden"],
["Thailand", "Singapore", "China", "Philippines", "Japan"],
["Zambia", "Egypt", "Kenya", "Nigeria", "Morocco"],
["Canada", "USA", "Mexico", "Jamaica", "Dominica"],
["Argentina", "Peru", "Colombia", "Brazil", "Chile"],
["Australia", "New Zealand", "Fiji", "Solomon Islands"]
]
var continentIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
let nib = UINib(nibName:"CountryCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "countryCell")
// Do any additional setup after loading the view.
}
func customInit(continentIndex: Int, title: String){
self.continentIndex=continentIndex
self.title=title
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryData[continentIndex].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "countryCell", for: indexPath) as! CountryCell
cell.textLabel?.text = countryData[continentIndex][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let provinceVC = ProvinceVC()
provinceVC.customInit(countryIndex: indexPath.row, title: countryData[continentIndex][indexPath.row])
self.navigationController?.pushViewController(provinceVC, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
}
以下是适当填充在第三个VC表视图中的数据中的代码:
import UIKit
class ProvinceVC: UIViewController, UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let provinceData = [
["Ger","ma","ny"], ["Fr","a","nce"], ["Ita","ly"], ["Spa","in"], ["Swe","den"],
["Tha","il","and"], ["Si","nga","po","re"], ["Chi","na"], ["Phi","li","pp","ines"], ["Ja","pan"],
["Za","mb","ia"], ["Egy","pt"], ["Ke","nya"], ["Ni","ge","ria"], ["Mo","ro","cco"],
["Ca","na","da"], ["USA"], ["Me","xi","co"], ["Ja","ma","ica"], ["Do","mi","ni","ca"],
["Ar","ge","nt","ina"], ["Pe","ru"], ["Co","lo","mb","ia"], ["Bra","zil"], ["Chi","le"],
["Au","st","ra","lia"], ["New","Zea","la","nd"],["Fi","ji"], ["So","lo","mon","Is","la","nds"]
]
var countryIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
let nib = UINib(nibName:"ProvinceCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "provinceCell")
// Do any additional setup after loading the view.
}
func customInit(countryIndex: Int, title: String){
self.countryIndex=countryIndex
self.title=title
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return provinceData[countryIndex].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "provinceCell", for: indexPath) as! ProvinceCell
cell.textLabel?.text = provinceData[countryIndex][indexPath.row]
return cell
}
}
我还需要提一下,因为使用真正的省名会太模糊,我决定分割国名,这些支离破碎的名字代表各省。
我的代码中可能出现什么问题?还有更好的方法吗?
答案 0 :(得分:1)
您不能将非洲大陆带到第三个视图控制器。进入第三视图控制器时,您知道非洲大陆的国家而不是大陆本身。
您的数据结构也存在不一致。
array
包含strings
)的数组。array
包含arrays
string
} array
包含arrays
arrays
个strings
) 您必须将非洲大陆传递给第三个控制器和/或找到更好的方式来存储和访问您的数据。