如何过滤获取的数据并加载到tableview中?

时间:2019-05-03 14:13:29

标签: ios arrays swift uitableview filter

我正在使用Firebase数据库。我获取了数据,只想过滤和显示与用户选择匹配的信息。

我已经使用条件语句来查找匹配的值并追加列表,但是,我无法加载该表。当我打印变量时,它打印0。需要帮助,谢谢。

   override func viewDidLoad() {
    super.viewDidLoad()

    projectlbl.text = "Brand: \((repair?.brand)!) Model: \((repair?.modelNumber)!)"

    refResources = Database.database().reference().child("resources");


    let selectedProject = "\((repair?.modelNumber)!)"


    //observing the data changes
    refResources!.observe(DataEventType.value, with: { (snapshot) in
        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self.resourceList.removeAll()

            //iterating through all the values
            for resources in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let resourceObject = resources.value as? [String: AnyObject]
                let id  = resourceObject?["id"]
                let RSSSubCat  = resourceObject?["RSSSubCat"]
                let RSSBrand = resourceObject?["RSSBrand"]
                let RSSModelNumber = resourceObject?["RSSModelNumber"]
                let RSSTitle = resourceObject?["RSSTitle"]
                let RSSUrl = resourceObject?["RSSUrl"]

                //creating resource object with model and fetched values
                let resource = resourceModel(id: id as! String?, RSSSubCat: RSSSubCat as! String?, RSSBrand: RSSBrand as! String?, RSSModelNumber: RSSModelNumber as! String?, RSSTitle: RSSTitle as! String?, RSSUrl: RSSUrl as! String?)

                //appending it to list
                print(snapshot.childrenCount)

                if selectedProject == resource.RSSModelNumber {
                    print(selectedProject)
                    self.resourceList.append(resource)
                    self.diagResTable.reloadData()
                } else {
                    return
                }

            }

            //reloading the tableview
            self.diagResTable.reloadData()
        }
    })

1 个答案:

答案 0 :(得分:0)

需要更改监听器...简单却被忽略了。

       let selectedModelNumber = (self.refResources?.queryOrdered(byChild: "RSSModelNumber").queryEqual(toValue: selectedProject))


    //observing the data changes
    selectedModelNumber!.observe(DataEventType.value, with: { (snapshot) in