Deleted Table View Cell Copying Remaining Tableview Cell Instead of Disappearing

I've got a UITableView that pulls some information from a Firebase Realtime Database. The information all gets pulled and populated properly, and deletes when it is supposed to, but I get a weird bug. If there are multiple cells in the table view, when I delete one of the cells, instead of that cell disappearing, it just gets replaced with a copy of one of the remaining cells. When I then close the table view and reopen it, everything is correct (i.e. the copied cell is gone). Here is the swift file for the table view:

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()
        }
        
    }
}

Why do you repeat reload line 11 ?

1.         if let spotKey = spot.key {
2.             Database.database().reference().child("Spots").child(spotKey).removeValue { (error, ref) in
3.                 if error != nil {
4.                     print("Failed to delete message:", error!)
5.                     return
6.                 }
7.                 self.spotsList.remove(at: indexPath.row)
8.                 self.tblSpots.deleteRows(at: [indexPath], with: .automatic)
9.                 self.tblSpots.reloadData()
10.             }
11.             tblSpots.reloadData()
12.         }

Try commenting out this line 11.

You know it's hard to reproduce something when external services such as Firebase are used.

So, this is just an impression glancing your code, but why are you calling unneeded reloadData()?

Generally, you have no need to call reloadData() when you properly call deleteRows(at:with:).

Please try removing reloadData() in tableView(_:commit:forRowAt:).

One more, you should better check the value of editingStyle in the method.

Sorry this is an older version of the code

Too bad 😉

So could you post the actual code that still fails ?

Deleted Table View Cell Copying Remaining Tableview Cell Instead of Disappearing
 
 
Q