gpt4 book ai didi

swift - 我希望我的选择器使用新子项重新加载 Firebase 数据

转载 作者:行者123 更新时间:2023-11-30 11:44:24 25 4
gpt4 key购买 nike

我正在尝试让我的选择器从 Firebase 重新加载数据。当查看时,它会响应文本Lbl 中的第一件事。我希望它也使用其他字符串之一重新加载。例如 I T 是出现的第一行,它已经设置在那里。我希望当我移动选择器并选择安全时它会发生变化。

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

}

最佳答案

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

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

修改数据库重新加载代码,以便不调用 insertRows(at indexPaths: [IndexPath], withanimation: UITableViewRowAnimation)。我想当您更改选择器中的值时,您将完全替换旧值,然后您不仅会插入新行。

关于swift - 我希望我的选择器使用新子项重新加载 Firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com