Post

Replies

Boosts

Views

Activity

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() } } }
3
0
955
Oct ’21