ios - UITableView 重新加载不使用 DispatchQueue.main.async 更新 tableview
问题描述
我已经用 swift4 编写了代码并在 iPhone 模拟器上运行。
DispatchQueue.main.async {
self.view.activityindicatorStop()
self.mytableView.reloadData()
}
上面的代码有以下操作
TableView
用数组填充数据后停止活动指示器并更新..
但预期不会发生..
我得到了关注
- 活动指示器停止但指示器未移除且背景视图未移除。
TableView
未更新。
我用过的代码:
super.viewDidLoad()
UserDefaults.lastAccessDate = Date()
self.view.activityStartAnimating(activityColor: #colorLiteral(red: 0.9176470588, green: 0.2901960784, blue: 0.262745098, alpha: 1), backgroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0))
//Calling the API
Capsulle.retriveSpaceTimeCapsulle { (data, respose, error) in
if nil == error
{
let ServerData = serverComm.toJSONObject(with: data!)
let result = ServerData.0 as? Array<Dictionary<String, Any>>
self.capsulleDetail = result
if (result == nil)
{
if false == loginCredential.isHavingAccessToken
{
loginCredential.isHavingAccessToken = false;
UserDefaults.standard.set( "", forKey: "UserEmailID")
let objStoryBoard:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let initialViewController:UIViewController = objStoryBoard.instantiateViewController(withIdentifier: "loginVC")
self.present(initialViewController, animated: true, completion: {
})
return
}
}
DispatchQueue.main.sync {
self.CapsulleCollectionView.reloadData()
self.view.activityStopAnimating()
}
}
}
}
func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor) {
let backgroundView = UIView()
backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
backgroundView.backgroundColor = backgroundColor
backgroundView.tag = 475647
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.color = activityColor
activityIndicator.startAnimating()
self.isUserInteractionEnabled = false
backgroundView.addSubview(activityIndicator)
self.addSubview(backgroundView)
}
func activityStopAnimating() {
if let background = self.viewWithTag(475647){
background.removeFromSuperview()
}
self.isUserInteractionEnabled = true
}
我创建了 activityStopAnimating 和 activityStartAnimating 作为扩展
解决方案
要重新加载 tableView,如果此数组是您的数据源,您必须确保self.capsulleDetail
数据已更改,我认为它没有更改或仍然为零,因此 reloadData() 将什么也不做
要隐藏指标,您应该设置此属性以在停止时隐藏它或通过将 is-hidden 设置为 true 直接隐藏它
self.activityIndicator.hidesWhenStopped = true
推荐阅读
- html - 我可以从 Angular 调用 Dialogflow 意图吗?
- rust - 在 Struct Vec 中查找结构的惯用方法,然后在 Rust 中对该结构执行特征函数
- git - 使用 BFG 清理 repo 后出现“未能推送一些 refs”错误
- javascript - 如何使用 opencv.js 对齐图像
- git - 使用常规提交更新包版本的好提交消息是什么?
- javascript - 将 Ajax 生成的 URL 传递到网页中的 JQuery 选项卡
- graph - Gnuplot:我可以在某些数据点上拉伸 x 轴吗?
- android - 在开发时检测Android App Crash和空指针异常
- google-cloud-platform - 谷歌云中计算机引擎响应缓慢
- apache-flink - pyflink(flink) 1.12.0 通过 to_append_stream (java api is: toAppendStream) 将表转换为数据流时出现错误