[英]Swift Firebase UIRefreshControl
我正在尝试重新加载我的表视图,以便用户可以获取与其位置相关的刷新数据。 我当前的设置是在执行“拉动刷新”操作时,它将开始将重复数据添加到表视图中,而不是清除它并将其替换为新数据。 我觉得有1行代码可以解决这个问题,但是我不知道它是什么,我觉得我之前在某处就已经看过它了……
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)
}
}
在方法getTableViewData
尝试执行usersArray.removeAll()
。
我也建议不要在handleRefresh
方法中执行以下操作:
self.tableView.reloadData()
refreshControl.endRefreshing()
原因:因为在使用getTableViewData()
方法获取数据后需要执行此操作。 实际上,您已经在getTableViewData()
方法的末尾重新加载了。 因此,也可以在那里调用refreshControl.endRefreshing()
。 但是您可能会要求不能从此方法访问该refreshControl,这意味着您需要使其成为全局属性。
如果您需要帮助,请在评论中让我知道。
我不明白您遇到的确切问题。 但是根据您发布的示例代码,您将在getTableViewData
方法userArray
数据追加到userArray
。 因此,无论何时调用handleRefresh
新数据userArray
与旧数据一起添加到userArray
。 首先在handleRefresh
清除userArray
的先前数据,然后调用方法getTableViewData
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.