问题描述
即使在先清空我的数组后我也得到重复的结果有人可以解释为什么会发生这种情况我试图在很多不同的地方清空这个数组 DriverOffers
但仍然得到相同的结果重复的 tableView 单元格到处都是.
func newOrders(){
//self.DriverOffers = [] here same result
let fireStore = Firestore.firestore()
let doc = fireStore.collection("الطلبات")
doc.addSnapshotListener { (query,err) in
if err != nil {
print(err?.localizedDescription ?? "")
}
//self.DriverOffers = []
query?.documentChanges.forEach({ (change) in
//self.DriverOffers = []
switch change.type{
case .added:
for document in query!.documents{
self.DriverOffers = []
let snap = document.data()
let name = snap["name"] as? String ?? ""
let phone = snap["phone"] as? String ?? ""
let time = snap["time"] as? String ?? ""
let marketName = snap["marketName"] as? String ?? "موقع محدد"
let price = snap["amount"] as? String ?? ""
dispatchQueue.main.async {
//self.DriverOffers = []
let offer = driverOrdersData(userUid: userid,name: name,phone: phone,amount: price)
self.DriverOffers.append(offer)
self.DriverOrdersTV.reloadData()
}
}
}
case .removed:
print("removed")
case .modified:
break
}
})
}
解决方法
在 tableViewCell 中使用这个方法并检查。
override func prepareForReuse() {
super.prepareForReuse()
self.nameLabel.text = nil
}