簡體   English   中英

當我從 tableview 單元格中刪除單元格時,未刪除的單元格會重復 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM