UIRefresh 控件 endRefreshing 不起作用
UIRefresh control endRefreshing doesn't work
当通过向下滑动表格视图触发刷新控件时,如果没有互联网连接,则会显示警报并且刷新控件预计会结束刷新,但即使添加到主线程中也不会结束刷新
class JobsForCategoryVC: UIViewController {
//MARK:-Outlets
@IBOutlet weak var jobTableView: UITableView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
//MARK:-Properties
var refreshControl:UIRefreshControl!
var jobCategory:JobCategoryDB!
var pageNumber:Int = 1
var downloadMore:Bool = true
var jobs = [JobModel]()
//MARK:-LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
setupView()
freshDownload()
}
func setupView(){
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Loading fresh Jobs")
refreshControl.addTarget(self, action: #selector(self.freshDownload), for: .valueChanged)
jobTableView.addSubview(refreshControl)
}
func freshDownload(){
pageNumber = 1
downloadMore = true
downloadJobsFrom(top: true)
}
func downloadJobsFrom(top:Bool){
if !refreshControl.isRefreshing && top{
activityIndicator.startAnimating()
}
let url = URLStringList.getSearchCategoryJobString(pageNumber: pageNumber, categoryId: jobCategory.id!)
if let url = URL(string: url){
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isSuccess{
let json = response.result.value
let model = Mapper<JobModel>().mapArray(JSONArray: json as! [[String : Any]])
if model?.count == 0{
self.downloadMore = false
}
if let jobs = model{
if top{
self.jobs = jobs
}else{
self.jobs += jobs
}
self.jobTableView.reloadData()
self.pageNumber += 1
}
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
}else{
self.activityIndicator.stopAnimating()
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.jobTableView.reloadData()
})
if top{
showInternetConnectionAlert(viewController: self, activityIndicator: self.activityIndicator, completion: nil)
}
}
}
}
}
}
extension JobsForCategoryVC:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if jobs.count > 0 {
jobTableView.backgroundView = nil
let cellCount = jobs.count + ((jobs.count-1)/(AdForNumberOfCells-1)) + 1
return cellCount
}
jobTableView.backgroundView = Bundle.main.loadNibNamed("PullToRefreshView", owner: nil, options: nil)?.first as? PullToRefreshView
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row % AdForNumberOfCells == 0{
if let cell = tableView.dequeueReusableCell(withIdentifier: "JobsAdTableViewCell", for: indexPath) as? JobsAdTableViewCell{
cell.controller = self
return cell
}
}else{
if let cell = tableView.dequeueReusableCell(withIdentifier: "JobsTableViewCell", for: indexPath) as? JobsTableViewCell{
let index = NSIndexPath(item: indexPath.row-(indexPath.row/AdForNumberOfCells)-1, section: 0)
cell.configure(job: jobs[index.row])
return cell
}
}
return UITableViewCell()
}
}
extension JobsForCategoryVC:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let webView = storyboard?.instantiateViewController(withIdentifier: "WKWebVC") as? WKWebVC{
if indexPath.row % AdForNumberOfCells == 0 {
return
}
let index = NSIndexPath(item: indexPath.row-(indexPath.row/AdForNumberOfCells)-1, section: 0)
if let urlString = jobs[index.row].url{
webView.url = urlString
webView.titleString = jobs[index.row].title
present(webView, animated: true, completion: nil)
}
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == self.jobs.count - 1 && downloadMore{
downloadJobsFrom(top: false)
}
}
}
请在主线程中添加所有 UI 个过程。
成功块 => 将您的代码变成:
if let jobs = model{
if top{
self.jobs = jobs
}else{
self.jobs += jobs
}
self.pageNumber += 1
}
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
失败块:
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
无论成功还是失败,你所有的 UI 进度都必须包含在主线程中 thread.I 以为你忘了在成功中包含 UI 主线程中的更改 Block.Or 你可以这样做,
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isSuccess{
...
}else{
...
}
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
}
看一看:
RefreshControlDemo [Swift 3 Xcode 8]
这样做的简单方法是
DispatchQueue.main.async(execute: {
self.jobTableView.reloadData()
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.refreshControl.setContentOffset(CGPoint.zero, animated: true)
})
将UITableView contentOffset
设置为zero
在Swift3.0
refreshControl.endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
我发现了同样的问题
如果另一个使用 self.refresh.endRefreshing()
不起作用
我引入这段代码 -> UIRefreshControl().endRefreshing()
替换状态 self.refresh.endRefreshing()
DispatchQueue.main.async {
UIRefreshControl().endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
}
谢谢
当通过向下滑动表格视图触发刷新控件时,如果没有互联网连接,则会显示警报并且刷新控件预计会结束刷新,但即使添加到主线程中也不会结束刷新
class JobsForCategoryVC: UIViewController {
//MARK:-Outlets
@IBOutlet weak var jobTableView: UITableView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
//MARK:-Properties
var refreshControl:UIRefreshControl!
var jobCategory:JobCategoryDB!
var pageNumber:Int = 1
var downloadMore:Bool = true
var jobs = [JobModel]()
//MARK:-LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
setupView()
freshDownload()
}
func setupView(){
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Loading fresh Jobs")
refreshControl.addTarget(self, action: #selector(self.freshDownload), for: .valueChanged)
jobTableView.addSubview(refreshControl)
}
func freshDownload(){
pageNumber = 1
downloadMore = true
downloadJobsFrom(top: true)
}
func downloadJobsFrom(top:Bool){
if !refreshControl.isRefreshing && top{
activityIndicator.startAnimating()
}
let url = URLStringList.getSearchCategoryJobString(pageNumber: pageNumber, categoryId: jobCategory.id!)
if let url = URL(string: url){
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isSuccess{
let json = response.result.value
let model = Mapper<JobModel>().mapArray(JSONArray: json as! [[String : Any]])
if model?.count == 0{
self.downloadMore = false
}
if let jobs = model{
if top{
self.jobs = jobs
}else{
self.jobs += jobs
}
self.jobTableView.reloadData()
self.pageNumber += 1
}
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
}else{
self.activityIndicator.stopAnimating()
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.jobTableView.reloadData()
})
if top{
showInternetConnectionAlert(viewController: self, activityIndicator: self.activityIndicator, completion: nil)
}
}
}
}
}
}
extension JobsForCategoryVC:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if jobs.count > 0 {
jobTableView.backgroundView = nil
let cellCount = jobs.count + ((jobs.count-1)/(AdForNumberOfCells-1)) + 1
return cellCount
}
jobTableView.backgroundView = Bundle.main.loadNibNamed("PullToRefreshView", owner: nil, options: nil)?.first as? PullToRefreshView
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row % AdForNumberOfCells == 0{
if let cell = tableView.dequeueReusableCell(withIdentifier: "JobsAdTableViewCell", for: indexPath) as? JobsAdTableViewCell{
cell.controller = self
return cell
}
}else{
if let cell = tableView.dequeueReusableCell(withIdentifier: "JobsTableViewCell", for: indexPath) as? JobsTableViewCell{
let index = NSIndexPath(item: indexPath.row-(indexPath.row/AdForNumberOfCells)-1, section: 0)
cell.configure(job: jobs[index.row])
return cell
}
}
return UITableViewCell()
}
}
extension JobsForCategoryVC:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let webView = storyboard?.instantiateViewController(withIdentifier: "WKWebVC") as? WKWebVC{
if indexPath.row % AdForNumberOfCells == 0 {
return
}
let index = NSIndexPath(item: indexPath.row-(indexPath.row/AdForNumberOfCells)-1, section: 0)
if let urlString = jobs[index.row].url{
webView.url = urlString
webView.titleString = jobs[index.row].title
present(webView, animated: true, completion: nil)
}
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == self.jobs.count - 1 && downloadMore{
downloadJobsFrom(top: false)
}
}
}
请在主线程中添加所有 UI 个过程。
成功块 => 将您的代码变成:
if let jobs = model{
if top{
self.jobs = jobs
}else{
self.jobs += jobs
}
self.pageNumber += 1
}
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
失败块:
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
无论成功还是失败,你所有的 UI 进度都必须包含在主线程中 thread.I 以为你忘了在成功中包含 UI 主线程中的更改 Block.Or 你可以这样做,
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isSuccess{
...
}else{
...
}
DispatchQueue.main.async(execute: {
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.jobTableView.reloadData()
})
}
看一看:
RefreshControlDemo [Swift 3 Xcode 8]
这样做的简单方法是
DispatchQueue.main.async(execute: {
self.jobTableView.reloadData()
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
self.refreshControl.setContentOffset(CGPoint.zero, animated: true)
})
将UITableView contentOffset
设置为zero
在Swift3.0
refreshControl.endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
我发现了同样的问题
如果另一个使用 self.refresh.endRefreshing()
不起作用
我引入这段代码 -> UIRefreshControl().endRefreshing()
替换状态 self.refresh.endRefreshing()
DispatchQueue.main.async {
UIRefreshControl().endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
}
谢谢