【发布时间】:2019-07-10 23:42:46
【问题描述】:
我正在将 firebase 实时数据库中的数据提取到我的 tableView 中,单击后的单元格会打开一个新视图,其中包含来自 firebase 的 tableView 中的数据,但我需要 tableView 来监听 firebase 数据的变化,所以我做了一个监听器,但是每次我关闭并重新打开视图控制器时,tableView 不会 reloadData() 它只是将新数据添加到 tableView 中的数组中而不删除旧的数组数据,这使得每次我关闭并重新打开视图时列表都会加倍控制器
我可以使用什么代码来删除旧的 tableView 数据并将新数据重新加载到其中,它将是相同的长度,如果不是,它将就像一个更新一样
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
dataService()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search NightClubs"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["All", "Barrie", "Toronto", "London"]
searchController.searchBar.delegate = self
tableView.tableFooterView = searchFooter
}
override func viewWillAppear(_ animated: Bool) {
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
super.viewWillAppear(animated)
}
func dataService() {
DataService.ds.REF_BARS.observe(.value, with: { (snapshot) in
print(snapshot.value as Any)
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
print(snap)
if let barData = snap.value as? Dictionary<String, AnyObject> {
let bar = NightClubs(barData: barData)
self.nightClubs.append(bar)
print(self.nightClubs)
self.tableView.reloadData()
}
self.tableView.reloadData()
}
self.tableView.reloadData()
self.smallerNightClubs = self.nightClubs//.filter { $0.promoted == "Yes"}
}
self.tableView.reloadData()
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
searchFooter.setIsFilteringToShow(filteredItemCount: filteredNightClubs.count, of: smallerNightClubs.count)
return filteredNightClubs.count
}
searchFooter.setNotFiltering()
return nightClubs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell") as! searchCell
let nightClub: NightClubs
if isFiltering() {
nightClub = filteredNightClubs[indexPath.row]
} else {
nightClub = nightClubs[indexPath.row]
}
cell.locationTextLabel.text = nightClub.location
cell.nameTextLabel.text = nightClub.name
return cell
}
【问题讨论】:
-
显示数据源数组以及如何在观察者中填充它
-
@Sh_Khan 我添加了它,它的数据服务,它只是将数据调用到 tableview 并重新加载
标签: swift xcode firebase uitableview firebase-realtime-database