2017-08-25 92 views
0

我正在尝试重新加载我的tableview,以便用户可以获取与其位置相关的刷新数据。我目前的设置已经到了当我进行“拉动刷新”时,它将开始向tableview添加重复数据,而不是清除它,并用新数据替换它。我觉得有1行代码,将解决这个问题,但我不知道它是什么,我觉得我以前在哪儿见过吧...Swift Firebase UIRefreshControl

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) 
    } 

} 
+0

你使用'userArray'填充tableview吗? – Amit

+0

是的,我正在使用userArray填充tableview –

+0

在'handleRefresh'方法中,尝试在使用'self调用'getTableViewData'前清除数组数据。 userArray.removeAllObjects()' – Amit

回答

0

getTableViewData尝试的方法做usersArray.removeAll()

此外,我会建议不要做这些内部handleRefresh方法:

self.tableView.reloadData() 
refreshControl.endRefreshing() 

为什么:因为你需要做它,你使用getTableViewData()方法获取数据后。实际上,您已经在getTableViewData()方法的末尾重新加载。所以只需拨打refreshControl.endRefreshing()即可。但是你可能会问这个refreshControl不能从这个方法中访问,这意味着你需要把它作为一个全局属性。

如果您需要帮助,请在评论中告诉我。

0

我不明白你有什么确切的问题。但根据您发布的示例代码,在方法getTableViewData中,您正在追加数据userArray。因此,无论何时您拨打handleRefresh,新数据都会在旧数据的userArray中增加。首先清除handleRefresh中的userArray的先前数据,然后调用方法getTableViewData