【问题标题】:Swift Firebase UIRefreshControlSwift Firebase UIRefreshControl
【发布时间】:2018-02-03 08:45:27
【问题描述】:

我正在尝试重新加载我的 tableview,以便用户可以获得与其位置相关的刷新数据。我当前的设置有它,当我执行“拉动刷新”时,它将开始向 tableview 添加重复数据,而不是清除它并用新数据替换它。我觉得有 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)
    }

}

【问题讨论】:

  • 您是否使用userArray 填充表格视图?
  • 是的,我正在用 userArray 填充 tableview
  • handleRefresh 方法中尝试在使用self. userArray.removeAllObjects() 调用getTableViewData 之前清除数组数据
  • 我不知道你能不能帮忙,但在我发布这个问题之前,我尝试了不同的事情并运行了“self.dataRef.removeValue()”,现在我的firebase数据不会再出现了
  • 我不明白您遇到的具体问题。但是根据您发布的示例代码,在方法@​​987654326@ 中,您将在userArray 中附加数据。因此,每当您调用 handleRefresh 时,新数据都会与旧数据一起添加到 userArray 中。先清除handleRefreshuserArray之前的数据,然后调用方法getTableViewData

标签: ios swift uitableview firebase refresh


【解决方案1】:

在方法getTableViewData里面尝试做usersArray.removeAll()

另外我建议不要在handleRefresh 方法中执行这些操作:

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

为什么:因为您需要在使用getTableViewData() 方法获取数据后执行此操作。实际上,您已经在 getTableViewData() 方法的末尾重新加载了。因此,也可以在此处调用refreshControl.endRefreshing()。但是你可能会问这个 refreshControl 不能从这个方法中访问,这意味着你需要将它作为一个全局属性。

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

【讨论】:

    【解决方案2】:

    我不明白您遇到的具体问题。但是根据您发布的示例代码,在方法@​​987654321@ 中,您将在userArray 中附加数据。因此,每当您调用 handleRefresh 时,新数据都会与旧数据一起添加到 userArray 中。先清除userArrayhandleRefresh中之前的数据,然后调用方法getTableViewData

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多