使用Swift 3拉动刷新和Alamofire [英] Pull to refresh and Alamofire using Swift 3
本文介绍了使用Swift 3拉动刷新和Alamofire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Alamofire从Web URL(JSON)获取数据.我正在尝试将RefreshControl
拉入我的项目中.我已经完成了,但是不知道它是否正确,或者刷新时数据是否正在更新.我的代码是:
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()
})
}
这正确吗?
推荐答案
您做得正确,但需要重新加载tableView
并在主线程上停止UIRefreshControl
.
You are doing correctly but you need reload tableView
and stop UIRefreshControl
on main thread.
DispatchQueue.main.async {
self.tableView.reloadData()
self.refresh.endRefreshing()
}
注意:,而不是始终在API请求上设置separatorStyle
,您需要使用viewDidLoad
或界面构建器"对其进行一次设置.
Note: Instead of setting separatorStyle
always on API request you need to set it once with viewDidLoad
or with Interface builder.
这篇关于使用Swift 3拉动刷新和Alamofire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文