2017-08-25 64 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