删除表视图单元格复制剩余的表视图单元格而不是消失



我有一个UITableView,从Firebase实时数据库中提取一些信息。所有信息都被正确地提取和填充,并在应该删除的时候删除,但我遇到了一个奇怪的错误。如果表视图中有多个单元格,当我删除其中一个单元格时,那个单元格不会消失,它只是被一个剩余单元格的副本所取代。然后,当我关闭表视图并重新打开它时,一切都是正确的(即复制的单元格消失了)。我在下面附上了一些照片作为说明。下面是表视图的swift文件:

import UIKit
import Firebase
import FirebaseAuth
class SpotRemove: ViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tblSpots: UITableView!

var spotsList = [ArtistModel]()

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return spotsList.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

let spot: ArtistModel

spot = spotsList[indexPath.row]

cell.lblName.text = spot.type
cell.lblGenre.text = spot.avail
cell.lblPrice.text = spot.price

return cell
}

var refSpots: DatabaseReference?

override func viewDidLoad() {
super.viewDidLoad()
tblSpots.allowsMultipleSelectionDuringEditing = true
refSpots = Database.database().reference().child("Spots")
refSpots?.observe(.value, with: { (snapshot) in
if snapshot.childrenCount>0{
for spots in snapshot.children.allObjects as![DataSnapshot]{
let spotKey = spots.key
let spotObject = spots.value as? [String: AnyObject]
let spotType = spotObject?["Type"]
let spotAvail = spotObject?["Availability"]
let spotPrice = spotObject?["Price"]
let spotID = spotObject?["UserID"]


let spot = ArtistModel(id: spotID as! String?, avail: spotAvail as! String?, type: spotType as! String?, price: spotPrice as! String?, key: spotKey as! String?)
let userID = Auth.auth().currentUser?.uid
if userID == spotID as? String {
self.spotsList.append(spot)
}
}
self.tblSpots.reloadData()
}
})
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

let spot = self.spotsList[indexPath.row]

if let spotKey = spot.key {
Database.database().reference().child("Spots").child(spotKey).removeValue { (error, ref) in
if error != nil {
print("Failed to delete message:", error!)
return
}
self.spotsList.remove(at: indexPath.row)
self.tblSpots.deleteRows(at: [indexPath], with: .automatic)
self.tblSpots.reloadData()
}
tblSpots.reloadData()
}

}
}

我认为问题是,我调用reloadData()没有改变numberOfRowsInSection。修复是否像在调用reload data之前将numberOfRowsInSection = numberOfRowsInSection - 1这样简单?提前感谢大家。

两个原始单元格

删除后的两个细胞

重新加载的表视图只显示剩余的单元格(因为它应该)

这是一种迂回的蛮力方式,但我决定让数组和表视图清除所有内容,然后从头重新加载它们。这是可行的(但对于优化效率来说并不完美)。整个delete函数现在看起来像这样:


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

let spot = self.spotsList[indexPath.row]

if let spotKey = spot.key {
Database.database().reference().child("Spots").child(spotKey).removeValue { (error, ref) in
if error != nil {
print("Failed to delete message:", error!)
return
}
self.spotsList.removeAll()
self.tblSpots.reloadData()

self.refSpots = Database.database().reference().child("Spots")
self.refSpots?.observe(.value, with: { (snapshot) in
if snapshot.childrenCount>0{
for spots in snapshot.children.allObjects as![DataSnapshot]{
let spotKey = spots.key
let spotObject = spots.value as? [String: AnyObject]
let spotType = spotObject?["Type"]
let spotAvail = spotObject?["Availability"]
let spotPrice = spotObject?["Price"]
let spotID = spotObject?["UserID"]


let spot = ArtistModel(id: spotID as! String?, avail: spotAvail as! String?, type: spotType as! String?, price: spotPrice as! String?, key: spotKey as! String?)
let userID = Auth.auth().currentUser?.uid
if userID == spotID as? String {
self.spotsList.append(spot)
}
}
self.tblSpots.reloadData()
}
})

}
}
}