简体   繁体   中英

Swift - disable default reload tableview in refresh control

I have UIViewController that has tableview as subview. 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. At the end of the getTableViewData function, I have a line of code to reload the 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:

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. 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

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.

If you want to trigger the data fetching manually, you should not add any targets to the refresh control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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