TableView会填充不正确的数据

问题描述 投票:0回答:1

我有三个视图控制器,每个都有一个表视图。在第一个VC的表中,V是各大洲的名称,在选择任何大陆名称时,用户被带到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
    }


}

我还需要提一下,因为使用真正的省名称太模糊了我决定分割国名,这些碎片名称代表各省。

我的代码中可能出现什么问题?有没有更好的方法呢?

ios swift3
1个回答
1
投票

您没有将大陆带到第三个视图控制器。进入第三视图控制器时,您知道该大陆的国家而不是大陆本身。

您的数据结构也存在不一致之处。

  • 你的第一个控制器有一个维度的数组(array包含strings)。
  • 你的第二个控制器更深一点,你有一个二维数组(array包含arraysstring
  • 你的第三个控制器是另一个步骤,但你仍然有一个二维数组,应该是一个三维数组(array包含arraysarrays strings

您必须通过非洲大陆到第三个控制器和/或找到更好的方式来存储和访问您的数据。

© www.soinside.com 2019 - 2024. All rights reserved.