0

我试图让我的选择器从 Firebase 重新加载数据。它在查看时响应 textLbl 中的第一件事。我也希望它使用其他字符串之一重新加载。例如 IT 是第一行出现它已经设置在那里。当我移动选择器并选择说安全时,我希望它改变。

    let officePerson = ["I T","SAFETY","H R/PAYROLL/BENEFITS DEPT","QUALITY","BUSINESS DEVELOPMENT SUPPORT","ACCOUNTING","RECRUITING","MAINTENANCE","SPECIAL PROJECTS","DISPATCH"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return officePerson[row]
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return officePerson.count
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerLbl.text = officePerson[row]
    self.view.endEditing(false)
       picker.reloadAllComponents()
    }

    var office = [OfficeModel]()

    @IBOutlet weak var tableView: UITableView!

    var databaseHandle:DatabaseHandle?

    var ref:DatabaseReference?

    var images = [String] ()

    var filteredArray = [OfficeModel]()
    var image = [OfficeModel] ()


    @IBOutlet weak var pickerLbl: UITextField!

    var picker = UIPickerView()

    var searchController = UISearchController(searchResultsController:nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        picker.delegate = self
        picker.dataSource = self
        pickerLbl.inputView = picker

        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension

        tableView.dataSource = self
        tableView.delegate = self

        filteredArray = image

        tableView.tableHeaderView = searchController.searchBar

        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.endEditing(true)
        searchController.definesPresentationContext = true

        Database.database().reference().child(pickerLbl.text!).observe(.childAdded) { (snapshot) in

            DispatchQueue.main.async {
                let newimages = OfficeModel(snapshot: snapshot)
                self.image.insert(newimages, at: 0)
                let indexPath = IndexPath(row: 0, section:0)
                self.tableView.insertRows(at: [indexPath], with: .top)
                self.tableView.reloadData()

                // self.refreshControl?.endRefreshing()
            }
        }
    }

    func updateSearchResults(for searchController: UISearchController){

        if searchController.searchBar.text! == ""{
            filteredArray = image
        } else{
            filteredArray = image.filter( {($0.name?.lowercased().contains(searchController.searchBar.text!.lowercased()))!} )

            // DispatchQueue.main.async {
            //    let newimages = Images(snapshot: snapshot)
            //   self.image.insert(newimages, at: 0)
            //   let indexPath = IndexPath(row: 0, section:0)
            //   self.tableView.insertRows(at: [indexPath], with: .top)
            //  self.tableView.reload
            self.tableView.reloadData()
        }    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.isActive {
            return filteredArray.count
        } else  {
            return image.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OfficeTableViewCell", for: indexPath) as! OfficeTableViewCell

        if searchController.isActive {

            cell.images = self.filteredArray[indexPath.row]
            cell.selectionStyle = .none
        } else {

            cell.images = self.image[indexPath.row]
            cell.selectionStyle = .none
        }

        return cell

    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 455
    }
    override func touchesBegan (_ touchs: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
4

1 回答 1

0

从从数据库加载数据的代码中创建一个函数。它现在被放置在 viewDidLoad 函数中,因此它只被调用一次。

pickerView(_:didSelectRow:inComponent:)设置pickerLbl.text值后,从 调用这个新函数。然后数据库重新加载代码将读取此值。

修改数据库重载代码,使您不调用insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation). 我猜您在更改选择器中的值时将完全替换旧值,然后您不仅插入新行。

于 2018-02-28T06:42:41.517 回答