简体   繁体   English

Swift-在刷新控件中禁用默认重载TableView

[英]Swift - disable default reload tableview in refresh control

I have UIViewController that has tableview as subview. 我有UIViewController拥有tableview作为子视图。 I assigned refresh control so that it refreshes a data. 我分配了刷新控件,以便刷新数据。

        refreshControl = UIRefreshControl()
        refreshControl.attributedTitle = NSAttributedString(string: "Loading...")
        refreshControl.addTarget(self, action: #selector(getTableViewData), for: .valueChanged)
        tableView.refreshControl = refreshControl

Inside the getTableViewData, I have multiple API calls to receive essentials data to be loaded in to the tableview. 在getTableViewData内部,我有多个API调用来接收要加载到tableview中的基本数据。 At the end of the getTableViewData function, I have a line of code to reload the tableview. 在getTableViewData函数的末尾,我有一行代码可以重新加载tableview。

The problem is that the refreshcontrol seems to trigger reload the table view as soon as it detects pull down. 问题在于,只要刷新控件检测到下拉列表,它似乎就会触发重新加载表视图。 I would like to disable this default pull down reload so that I can first gather all the essential data and then reload. 我想禁用此默认下拉重载,以便我可以首先收集所有基本数据,然后重载。 Is there anyway I can disable the default reload by anyways? 无论如何,无论如何我都可以禁用默认重载吗?

Inside the getTableViewData, code looks like: 在getTableViewData内部,代码如下:

var fetchGroup = DispatchGroup()
@objc func getReceipts(){
    self.refreshControl.beginRefreshing()
    self.dataSet1.removeAll()
    self.dataSet2.removeAll()
    self.dataSet3.removeAll()

    fetchGroup.enter()
    fetchGroup.enter()
    fetchGroup.enter()

    if let fetch1URL = URL(string: "fetchDataSet1", relativeTo: Property.baseURL){
        let headers: HTTPHeaders = [
            "Authorization": Property.shared.token,
            "content-type": "application/json"
        ]
        Property.alamofireManager.request(fetch1URL, headers: headers).responseJSON{ response in
            //Append json data into dataSet1 here...
            self.fetchGroup.leave()
        }
    }else{
        self.fetchGroup.leave()
    }

    if let fetchURL2 = URL(string: "fetchDataSet2", relativeTo: Property.baseURL){
        let headers: HTTPHeaders = [
            "Authorization": Property.shared.token,
            "content-type": "application/json"
        ]
        let params: Parameters = [
            "Temp":""
        ]
        Property.alamofireManager.request(fetchURL2, parameters: params, headers: headers).responseJSON{ response in
            //Append json data into dataSet2 here...
            self.fetchGroup.leave()
        }
    }else{
        self.fetchGroup.leave()
    }

    if let fetch3URL = URL(string: "fetchDataSet3", relativeTo: Property.baseURL){
        let headers: HTTPHeaders = [
            "Authorization": Property.shared.token,
            "content-type": "application/json"
        ]
        let params: Parameters = [
            "Temp":""
        ]
        Property.alamofireManager.request(fetch3URL, parameters: params, headers: headers).responseJSON{ response in
            //Append json data into dataSet3 here...
            self.fetchGroup.leave()
        }
    }else{
        self.fetchGroup.leave()
    }

    fetchGroup.notify(queue: .main, execute: {
        self.refreshControl.endRefreshing()
        print("Going to Refresh")
        self.tableView.reloadData()
    })
}

Thank You! 谢谢!

You can use dispatch groups to make the API calls. 您可以使用调度组进行API调用。 With the help of dispatch groups, you can be notified when the calls are completed. 借助调度组,可以在呼叫完成时通知您。 At the completion of the calls, you can check whether there are any errors or not. 通话结束后,您可以检查是否有任何错误。 If there are no errors, you can reload the data in the cells. 如果没有错误,您可以在单元格中重新加载数据。

Please check this link for reference: https://dispatchswift.com/introduction-to-dispatch-group-a5cf9d61ff4f 请检查此链接以供参考: https : //dispatchswift.com/introduction-to-dispatch-group-a5cf9d61ff4f

By default when you add a UIRefreshControl to a UITableView , the refresh control will animate and refresh when you pull down the tableView and if you have added a target to the refresh control with an action, the selectors' method will be called. 默认情况下,当您将UIRefreshControl添加到UITableView ,当您下拉tableView时,刷新控件将设置动画并刷新,并且如果您通过操作将目标添加到刷新控件中,则将调用选择器的方法。

If you want to trigger the data fetching manually, you should not add any targets to the refresh control. 如果要手动触发数据获取,则不应将任何目标添加到刷新控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM