Pull to refresh and Alamofire using Swift 3

青春壹個敷衍的年華 提交于 2019-12-25 08:15:41

问题


I am using Alamofire to get data from a web URL(JSON). I am trying to implement pull to RefreshControl into my project. I have done it but don't know if it is correct or if the data is getting updated when refreshed. My code is:

var refresh = UIRefreshControl()

refresh.addTarget(self, action: #selector(self.refreshData), for: UIControlEvents.valueChanged)

func refreshData() {

    Alamofire.request("https://www.example.com/api").responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine

        self.tableView.reloadData()
        self.refresh.endRefreshing()


    })        
}

Is this correct?


回答1:


You are doing correctly but you need reload tableView and stop UIRefreshControl on main thread.

DispatchQueue.main.async {
    self.tableView.reloadData()
    self.refresh.endRefreshing()             
}

Note: Instead of setting separatorStyle always on API request you need to set it once with viewDidLoad or with Interface builder.



来源:https://stackoverflow.com/questions/40786546/pull-to-refresh-and-alamofire-using-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!