0

I'm trying to get my picker to reload the data from Firebase. It responds to the first thing in the textLbl when viewed. I would like it to reload using one of the other strings as well. example I T is the first line to appear its already set there. I would like it to change when I move the selector an choose say safety.

    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.
    }
    */

}
Pang
  • 9,564
  • 146
  • 81
  • 122
Issac
  • 3
  • 3

1 Answers1

0

Create a function from the piece of code that loads data from database. It is now placed in viewDidLoad function so it gets called only once.

Call this new function from pickerView(_:didSelectRow:inComponent:), after you have set pickerLbl.text value. Database reloading code will read this value then.

Modify database reloading code so that you do not call insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation). I guess you will be replacing old values altogether when changing value in picker, and then you are not only inserting new rows.

Mika
  • 1,256
  • 13
  • 18