【问题标题】:When I delete cell from tableview cells that are not deleted are duplicated SWIFT当我从 tableview 中删除单元格时,未删除的单元格被重复 SWIFT
【发布时间】:2021-03-05 07:57:48
【问题描述】:

我有一个使用 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 可选的(“yakalaaa”) 可选的(“xyzzz”) 删除预订列表后 : 1 可选的(“yakalaaa”)

这是我用 firebase 数据加载的 tableView: enter image description here

我删除第二个单元格:

enter image description here

这就是问题所在:

enter image description here

有什么建议吗?

【问题讨论】:

  • 不,我查过了。我可以删除所选项目stationRef.child(reservationTime).setValue(nil)
  • 问题不在此代码中。您还有其他附加到reservationList 的地方吗?你在控制台得到什么输出?
  • 不,我唯一追加的地方是 loadpost 函数。这是删除方法有效时的输出。删除前 Count of reservation list : 2 Optional("yakalaaa") Optional("xyzzz") 删除后 Count of reservation list : 1 Optional("yakalaaa")
  • 然后显示你的tableview方法(行数和cellForRow)。
  • 另外,在你的观察者闭包中放置一个调试打印或设置一个断点来告诉你它何时被调用。我不使用 Firestore,但我认为您没有正确使用它。

标签: ios swift uitableview


【解决方案1】:

问题出在以下几行:

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

直接使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多