使用另一个字符串数组搜索Firestore数组并在tableview中显示结果

时间:2019-05-09 14:00:42

标签: ios arrays swift uitableview google-cloud-firestore

我正在制作一个应用,其中我要使用另一个字符串数组搜索Firestore数组。 可以说我有2个Firestore数组。他们叫compName。

from_pydot

https://ibb.co/sgL9fDM

我想根据下面的数组查询这两个数组

var testTable = [“ ci 77007”,“ ultramarines”,“ ci 77491”,“ ci 77492”,“ ci 77499”,“氧化铁”,“ ci 77891”,“纤维素”,“ 15375-21- 0“]

,然后在表格视图中显示成分。在我们的例子中是15375-21-0和纤维素。

这是我的代码

class SearchTableView: UITableViewController {

    var ingredientsArray = [Ingredients]()

   var testTable = ["ci 77007", "ultramarines", "ci 77491", "ci 77492", "ci 77499", "iron oxides", "ci 77891", "cellulose", "15375-21-0"]


    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()


      }

    func loadData(){

        for i in 0..<testTable.count {
        let db = Firestore.firestore()

        db.collection("Ingredients").whereField("compName", arrayContains: testTable[i] ).getDocuments(){
            querySnapshot, error in
            if let error = error {
                print("\(error.localizedDescription)")
            }else if let querySnapshot = querySnapshot {
                self.ingredientsArray = querySnapshot.documents.compactMap({Ingredients(dictionary: $0.data())})
                DispatchQueue.main.async {
                    self.tableView.reloadData()

                }
            }
        }
    }
}

这是表格视图的代码

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return ingredientsArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let item = ingredientsArray[indexPath.section].compName[indexPath.row]
        cell.textLabel!.text = ("\(item)")

        return cell
    }

    override func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {

        let off = scrollView.contentOffset.y
        let off1 = scrollView.contentSize.height

    }
}

我的结构

struct Ingredients{
    var allergies:String
    var cancer:String
    var compName: Array<String>
    var description:String
    var hazardScore:String
    var hazard_Statements:String
    var irritation:String
    var pharmacological_Classification:String
    var pubchemId:String
    var toxicity:String

    var dictionary: [String :Any]{
        return[
            "allergies":allergies,
            "cancer":cancer,
            "compName":compName,
            "description":description,
            "hazardScore":hazardScore,
            "hazard_Statements":hazard_Statements,
            "irritation":irritation,
            "pharmacological_Classification":pharmacological_Classification,
            "pubchemId":pubchemId,
            "toxicity":toxicity


        ]
    }
}

extension Ingredients{

    init?(dictionary:[String:Any]){
        guard let allergies = dictionary["allergies"] as? String,
            let cancer = dictionary["cancer"] as? String,
            let compName = dictionary["compName"] as? Array<String>,
            let description = dictionary["description"] as? String,
            let hazardScore = dictionary["hazardScore"] as? String,
            let hazard_Statements = dictionary["hazard_Statements"] as? String,
            let irritation = dictionary["irritation"] as? String,
            let pharmacological_Classification = dictionary["pharmacological_Classification"] as? String,
            let pubchemId = dictionary["pubchemId"] as? String,
            let toxicity = dictionary["toxicity"] as? String else{ return nil }

self.init(allergies: allergies, cancer: cancer, compName: compName, description: description, hazardScore: hazardScore, hazard_Statements: hazard_Statements, irritation: irritation, pharmacological_Classification: pharmacological_Classification, pubchemId: pubchemId, toxicity: toxicity)

    }
}

问题在于它仅显示数组的最后成分,而不显示找到的其他成分。 它可能是for循环,仅保存数组的最后一个元素。有办法改变吗?

0 个答案:

没有答案