[英]The last cell in a tableview cell should not be deleted when trying to remove from array in swift
[英]When I delete cell from tableview cells that are not deleted are duplicated SWIFT
我有一个使用 firebase 数据创建的 tableView。
这是我的负载功能:
var reservationList = [UserModal]()
private func loadposts() {
reservationList = []
activityIndicator.startAnimating()
Database.database().reference().child("BookReservations").observe(.value) { snapshot in
for case let child as DataSnapshot in snapshot.children {
guard let dict = child.value as? [String:Any] else {
print("Error")
self.activityIndicator.stopAnimating()
return
}
let name = dict["name"] as! String
let date = dict["date"] as? String ?? "nil"
let book = dict["bookName"] as? String ?? "nil"
let FullDate = dict["fullDate"] as? String ?? "nil"
let phoneNumber = dict["phoneNumber"] as? String ?? "nil"
let reservations = UserModal(name: name, dateAndTime: date, choosenBook: bookName , phoneNumber: phoneNumber, tamRandevu: fullDate)
self.reservationList.append(reservations)
print(self.reservationList)
self.activityIndicator.stopAnimating()
self.tableView.reloadData()
}
}
}
extension ReservationListViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reservationList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reservationCell", for: indexPath) as! reservationCell
cell.bookNameLabel.text = reservationList[indexPath.row].choosenBook
cell.nameLabel.text = reservationList[indexPath.row].userName
cell.dateAndTimeLabel.text = reservationList[indexPath.row].fullDate
cell.phoneButton.setTitle(reservationList[indexPath.row].phoneNumber, for: .normal)
return cell
}
我可以获得价值并显示在 tableView 上。 但是当我尝试删除单元格时:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
activityIndicator.startAnimating()
let reservationTime = reservationList[indexPath.row].fullDate ?? "nil"
print(reservationTime)
if reservationTime != "nil" {
let stationsRef = database.child("BookReservations")
print(reservationList.count)
print("Before deleting Count of reservation list : \(reservationList.count)")
reservationList.forEach { (UserModal) in
print(UserModal.name)
}
stationsRef.child(reservationTime).setValue(nil)
reservationList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print("After deleting Count of reservation list : \(reservationList.count)")
reservationList.forEach { (UserModal) in
print(UserModal.name)
}
activityIndicator.stopAnimating()
}else{
activityIndicator.stopAnimating()
print("no reservation found.")
}
}
}
我可以成功地从 firebase 实时数据库中删除数据。 但是当我删除数据的时候,如果tableView中有多个数据,未删除的数据就会重复。 我尝试检查我的数组计数,当删除计数减少时它似乎很好。我尝试重新加载 tableview,开始更新和更新,但没有任何效果。 仅当我使用刷新方法刷新 tableview 时才有效。 这是我的打印输出:
删除预约列表计数前:2 Optional("yakalaaa") Optional("xyzzz") 删除预约列表计数后:1 Optional("yakalaaa")
这是我用 firebase 数据加载的 tableView:在此处输入图像描述
我删除第二个单元格:
这就是问题所在:
有什么建议 ?
问题在于这些行:
tableView.beginUpdates()
reservationList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .bottom)
tableView.endUpdates()
只是使用代替
reservationList.remove(at: indexPath.row)
tableView.reloadData()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.