I have delete functionality in my app for favourites but when I delete a row but add another a favourite that row I just deleted is just added back to the top (gotta make sure that does not happen). I think this has to do with the notification.
class FavouriteManager {
static let shared = FavouriteManager()
//it's an array now
var favArr : [CurrentPlayers] = []
var noRepFav : [CurrentPlayers] = []
func add(_ player: CurrentPlayers) {
favArr.append(player)
for player in favArr {
if !noRepFav.contains(player) {
noRepFav.append(player)
}
}
NotificationCenter.default.post(
name: .passFavNotification,
object: player
)
}
}
class FavouritesVC: UITableViewController {
var prefArr: Array<CurrentPlayers> {
get { FavouriteManager.shared.noRepFav }
set { FavouriteManager.shared.noRepFav = newValue }
}
@objc
func handleFavNotification(notification: Notification) {
tableView.reloadData()
}
//delete function
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
prefArr.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}