简体   繁体   English

当我从 tableview 单元格中删除单元格时,未删除的单元格会重复 SWIFT

[英]When I delete cell from tableview cells that are not deleted are duplicated SWIFT

I have a tableView which I created with firebase data.我有一个使用 firebase 数据创建的 tableView。

Here is my load func :这是我的负载功能:

  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
    }

I can get value and show on tableView.我可以获得价值并显示在 tableView 上。 But when I try to delete cell :但是当我尝试删除单元格时:

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.")
            }
        
      }
    }

I can succesfully delete data from firebase real time database.我可以成功地从 firebase 实时数据库中删除数据。 But when I delete data , If there is more than one data in the tableView, the non-deleted data will repeat itself.但是当我删除数据的时候,如果tableView中有多个数据,未删除的数据就会重复。 I try to check my array count it seems good when delete count is decreasing.. I try tableview reload, begin updates and and updates but nothing works.我尝试检查我的数组计数,当删除计数减少时它似乎很好。我尝试重新加载 tableview,开始更新和更新,但没有任何效果。 Only works when I refresh tableview with refresh method.仅当我使用刷新方法刷新 tableview 时才有效。 This is my print outputs :这是我的打印输出:

Before deleting Count of reservation list : 2 Optional("yakalaaa") Optional("xyzzz") After deleting Count of reservation list : 1 Optional("yakalaaa")删除预约列表计数前:2 Optional("yakalaaa") Optional("xyzzz") 删除预约列表计数后:1 Optional("yakalaaa")

this is my tableView which I load with firebase data : enter image description here这是我用 firebase 数据加载的 tableView:在此处输入图像描述

I delete second cell :我删除第二个单元格:

enter image description here在此处输入图片说明

and this is the problem :这就是问题所在:

enter image description here在此处输入图片说明

any suggestions ?有什么建议 ?

the problem is with these lines:问题在于这些行:

tableView.beginUpdates()
reservationList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .bottom)
tableView.endUpdates()

just use instead只是使用代替

reservationList.remove(at: indexPath.row)
tableView.reloadData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从数组中快速删除时,不应删除 tableview 单元格中的最后一个单元格 - The last cell in a tableview cell should not be deleted when trying to remove from array in swift 如何从Swift 2的tableView中清除已删除的表格视图单元格 - How do I clear a deleted table view cell from a tableView in swift 2 Swift TableView删除单元格未显示 - Swift TableView Delete Cell not displaying TableView 单元格重复/复制 - TableView cells are repeated/duplicated 当我在 swift iOS 中垂直滚动时,Tableview 单元格数据正在发生变化 - Tableview cells data is changing when I scroll vertically in swift iOS 在Swift中,如何保存自定义视图中的数据,以便在单元出队时不会被删除? - In Swift, how should I save data from a custom view so that it is not deleted when a cell is dequeued? 当我从TableView上的按钮操作中删除单元格时,应用程序崩溃了-iOS Swift - When i remove cell from TableView on button action the app is crashed - iOS Swift 当我单击没有元素的Swift时,Tableview单元格只会显示 - Tableview cell only segues when I click where there are no elements Swift 从View控制器导航烘焙时删除tableview单元格 - delete tableview cell when navigation bake from View controller 使用 ImageSlideshow pod 时显示上一个 tableview 单元格中的重复图像 - showing duplicated images from previous tableview cell when using ImageSlideshow pod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM