遍历对象数组和基于属性的过滤器

时间:2019-06-24 23:07:37

标签: ios arrays swift

我目前正在将一组对象从一个VC传递到另一个VC。这很好,因为我可以传递数据,但是在 ViewDidLoad()中,当我遍历数组并将其追加到新数组时,新数组始终为空。

我在stackoverflow上经历了几个答案,但是所有这些都只是在暗示我尝试过的方法,应该可以工作,例如用于循环或使用过滤器。我已经在PLaygrounds中复制了文件,并且在那儿似乎还可以,所以不确定是怎么回事?

简要分类:

  1. 使用附加到第一个数组的数据模型在初始viewcontroller中配置了FirebaseDB
  2. 使用准备进行转储
  3. 传递的数据
  4. ViewDidLoad()中循环遍历对象数组,如果特定属性匹配,则使用过滤器追加到新数组(我尝试使用for循环和if语句,效果类似)

数据模型:

 struct resortData {

    var resort: String = ""
    var passengerName: String = ""
    var hotel: String = ""
    var rmNumber: Int = 0
  }

ResortVC(初始VC):

class ResortViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var resortArray: [resortData] = [resortData]()
    var currentResort: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        SVProgressHUD.show()

        title = "Resort"

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(loggedOut))

        tableView.delegate = self
        tableView.dataSource = self

        configureDB()

    }


    @objc func loggedOut() {

        do {
            try Auth.auth().signOut()
    navigationController?.popToRootViewController(animated: true)

        } catch {
            print("Error signing out")
        }
    }


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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resortArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.resortLabel.text = resortArray[indexPath.row].resort
        cell.accessoryType = .disclosureIndicator
        cell.resortCountryLocation.text = "Spain"
        cell.resortImg.layer.cornerRadius = 10.0
        cell.resortImg.image = UIImage(named: resortArray[indexPath.row].resort + ".png")
        return cell
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.alpha = 0

        UIView.animate(withDuration: 0.5) {
            cell.alpha = 1
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        currentResort = resortArray[indexPath.row].resort

        performSegue(withIdentifier: "goToPassengerDetail", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToPassengerDetail" {

                    let detailVC = segue.destination as! ResortDetailTableViewController
                    detailVC.passengers = resortArray
                    detailVC.selectedResort = currentResort    
        }
    }


    func configureDB() {

        let tableData = Database.database().reference().child("masterSheet")

        tableData.observe(.childAdded) { (snapshot) in

            let snapshotValue = snapshot.value as! NSArray
            let resort = snapshotValue[0] as! String
            let name = snapshotValue[1] as! String
            let hotel = snapshotValue[5] as! String
            let roomNumber = snapshotValue[6] as! Int

            var resortDB = resortData()
            resortDB.resort = resort
            resortDB.passengerName = name
            resortDB.hotel = hotel
            resortDB.rmNumber = roomNumber

            self.resortArray.append(resortDB)
            self.tableView.reloadData()
            SVProgressHUD.dismiss()

        }
    }


}

DetailVC:


    class ResortDetailTableViewController: UITableViewController {

        var passengers = [resortData]()
        var selectedResort: String?
        var filteredPassengerArray = [resortData]()

        override func viewDidLoad() {
            super.viewDidLoad()
            title = "Passenger Info ?"
            print(selectedResort ?? "No data passed over")

            filteredPassengerArray = passengers.filter { $0.hotel == selectedResort }
            print(filteredPassengerArray)
        }

filteredPassengerArray 仅应包含具有 passengers.hotel

特定属性的对象

0 个答案:

没有答案