I am trying to reload my tableview so users can get refreshed data related to their location. My current setup has it to where when I do a "pull to refresh" it will start adding duplicate data to the tableview instead of clearing it and replacing it with the new data. I feel like there is 1 line of code that will solve this problem but I don't know what it is, I feel like I've seen it before somewhere...
var refresher: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
locationManager.delegate = self
locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.stopUpdatingLocation()
getTableViewData()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher.addTarget(self, action: #selector(RegisteredLocationsTableView.handleRefresh(refreshControl:)), for: UIControlEvents.valueChanged)
tableView.addSubview(refresher)
}
func handleRefresh(refreshControl: UIRefreshControl) {
getTableViewData()
self.tableView.reloadData()
refreshControl.endRefreshing()
}
func getTableViewData() {
databaseRef.child("Businesses").queryOrdered(byChild: "businessName").observe(.childAdded, with: { (snapshot) in
let key = snapshot.key
if(key == self.loggedInUser?.uid) {
print("Same as logged in user, so don't show!")
} else {
if let locationValue = snapshot.value as? [String: AnyObject] {
let lat = Double(locationValue["businessLatitude"] as! String)
let long = Double(locationValue["businessLongitude"] as! String)
let businessLocation = CLLocation(latitude: lat!, longitude: long!)
let latitude = self.locationManager.location?.coordinate.latitude
let longitude = self.locationManager.location?.coordinate.longitude
let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)
let distanceInMeters : Double = userLocation.distance(from: businessLocation)
let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137)
let distanceLabelText = "\(distanceInMiles.string(2)) miles away"
var singleChildDictionary = locationValue
singleChildDictionary["distanceLabelText"] = distanceLabelText as AnyObject
singleChildDictionary["distanceInMiles"] = distanceInMiles as AnyObject
self.usersArray.append(singleChildDictionary as NSDictionary)
self.usersArray = self.usersArray.sorted {
!($0?["distanceInMiles"] as! Double > $1?["distanceInMiles"] as! Double)
}
}
//insert the rows
//self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
self.followUsersTableView.reloadData()
}
}) { (error) in
print(error.localizedDescription)
}
}
Inside the method getTableViewData
try to do usersArray.removeAll()
.
Also I would suggest not doing these inside handleRefresh
method:
self.tableView.reloadData()
refreshControl.endRefreshing()
Why: because you need to do it after you get the data using getTableViewData()
method. And actually, you already do reload in the end of the getTableViewData()
method. So just call also this there refreshControl.endRefreshing()
. But you may ask that this refreshControl is not accessible from this method, that means you need to make it as a global property.
Let me know in comments if you need help.
I don't understand what exact issue you are having. But according to the sample code you have posted, in method getTableViewData
you are appending data in userArray
. So, whenever you are calling handleRefresh
new data is getting added in userArray
with old data. First clear the previous data of userArray
in handleRefresh
then call method getTableViewData
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.