Ad

How To Clear TableView Data And ReloadData Every Time View Loads Again

I am fetching data from firebase realtime database into my tableView and the cells upon click open a new view with the data from the tableView from firebase, but I need the tableView to listen for changes in the firebase data so I made a listener, but every time I close and reopen the viewcontroller the tableView doesn't reloadData() it just adds the new data to the array in the tableView without deleteing the old array data, this makes the list double every time I close and reopen the view controller

what code can I use to remove the old tableView data and reload the new into it, it'll be the same length and if not it'll just be like an update

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.dataSource = self
    tableView.delegate = self

    dataService()

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search NightClubs"

    navigationItem.searchController = searchController
    definesPresentationContext = true

    searchController.searchBar.scopeButtonTitles = ["All", "Barrie", "Toronto", "London"]
    searchController.searchBar.delegate = self
    tableView.tableFooterView = searchFooter
}

override func viewWillAppear(_ animated: Bool) {
    if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
        self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
super.viewWillAppear(animated)
}

func dataService() {
    DataService.ds.REF_BARS.observe(.value, with: { (snapshot) in
        print(snapshot.value as Any)
        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
            for snap in snapshot {
                print(snap)
                if let barData = snap.value as? Dictionary<String, AnyObject> {
                    let bar = NightClubs(barData: barData)
                    self.nightClubs.append(bar)
                    print(self.nightClubs)
                    self.tableView.reloadData()
                }
                self.tableView.reloadData()
            }
            self.tableView.reloadData()
            self.smallerNightClubs = self.nightClubs//.filter { $0.promoted == "Yes"}
        }
        self.tableView.reloadData()
    })
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        searchFooter.setIsFilteringToShow(filteredItemCount: filteredNightClubs.count, of: smallerNightClubs.count) 
        return filteredNightClubs.count
    }
    searchFooter.setNotFiltering()
    return nightClubs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell") as! searchCell
    let nightClub: NightClubs
    if isFiltering() {
        nightClub = filteredNightClubs[indexPath.row]
    } else {
        nightClub = nightClubs[indexPath.row]
    }
    cell.locationTextLabel.text = nightClub.location
    cell.nameTextLabel.text = nightClub.name
    return cell
}
Ad

Answer

You need to clear the array before next append like

self.nightClubs.removeAll()
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
   for snap in snapshot {
Ad
source: stackoverflow.com
Ad