【发布时间】:2018-01-08 16:32:22
【问题描述】:
我意识到这有点乱,但我正在通过将教程和参考资料拼凑在一起来了解事情的工作原理。 我的问题是当我从 firebase 中删除一个节点时,tableview 没有更新。它已从 firebase 中删除,但单元格仍保留在 tableview 中。虽然下面没有显示,但我尝试在我认为应该去的每个地方添加 mytableview.reload 代码块,但我没有成功。任何意见表示赞赏。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventsList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ViewControllerTableViewCell
cell.locationLabel.text = eventsList[indexPath.row].location
cell.eventTimeLabel.text = eventsList[indexPath.row].dateTime
cell.eventTypeLabel.text = eventsList[indexPath.row].agencyEventSubtypeCode
cell.agencyIdLabel.text = eventsList[indexPath.row].agencyId
if eventsList[indexPath.row].alarmLevel == "1" {
cell.locationLabel.textColor = UIColor.red
cell.eventTimeLabel.textColor = UIColor.red
cell.eventTypeLabel.textColor = UIColor.red
cell.agencyIdLabel.textColor = UIColor.red
}
return cell
}
ref = Database.database().reference()
fetchData()
}
func fetchData(){
refHandle = ref?.child("caddata").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: Any] {
let event = Events()
event.setValuesForKeys(dictionary)
if event.originatingAction == "CadEventNew" {
self.eventsList.append(event)
DispatchQueue.main.async {
self.myTableView.reloadData()
}
} else if event.originatingAction == "CadCloseEvent" {
self.cadPacketType = event.originatingAction
self.cadEventNumber = event.agencyEventId
self.queryCloseUIDFromDB()
}
}
})
}
func queryCloseUIDFromDB(){
if cadPacketType == "CadCloseEvent" {
let dataRef = ref?.child("caddata")
let queryRef = dataRef?.queryOrdered(byChild: "agencyEventId")
.queryEqual(toValue: cadEventNumber)
queryRef?.observeSingleEvent(of: .value, with: { (snapshot) in
for snap in snapshot.children {
let dataSnap = snap as! DataSnapshot
self.closeUID = dataSnap.key //the uid of each event
if self.closeUID != nil {
self.deleteFromDB()
DispatchQueue.main.async {
self.myTableView.reloadData()
}
}
}
})
}
}
func deleteFromDB () {
if closeUID != nil {
print (cadEventNumber!)
print (closeUID!)
ref?.child("caddata").child(closeUID!).removeValue(completionBlock: { (error, ref) in
if error != nil {
self.fetchData()
print("Error: \(String(describing: error))")
return
}
})
}
print ("\(String(describing: closeUID)) has been removed")
}
【问题讨论】:
-
你能发布你的
UITableViewDataSource方法吗? -
在您的
deleteFromDB方法中,您需要更新您的数组并调用tableView.reloadData() -
你能指出我如何更新数组的正确方向吗?我在原帖中添加了tableview函数
-
调用
fetchData方法如果在你删除值的闭包中没有出现错误,请告诉我 -
我尝试了 fetchData 的想法,但没有奏效。当我从地图 segue 返回时,它会正确重新加载,但在 deleteFromDB func 的 if 语句中调用 fetchData 时则不会
标签: ios swift firebase firebase-realtime-database