[英]UITableView Reload Not Updating the tableview with DispatchQueue.main.async
我已经用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作为扩展
DispatchQueue.main.async {
self.view.activityIndicator.stopAnimating()
self.view.activityIndicator.isHidden = true
self.mytableView.beginUpdates()
self.mytableView.endUpdates()
}
您的代码应该适用于tableView
重新加载,但是您也可以尝试使用beginUpdates()
和endUpdates()
方法。 并要stopActivityindicator调用stopAnimating()
,然后隐藏您的activityindicator,此代码应为您工作
享受编码。
要重新加载tableView,必须确保self.capsulleDetail
数据已更改(如果此数组是您的数据源),我认为它没有更改或仍为零,因此reloadData()不会执行任何操作
要隐藏指示器,您应该将此属性设置为在停止时隐藏它,或者通过将is-hidden-hidden隐藏为true来直接隐藏它
self.activityIndicator.hidesWhenStopped = true
我找不到任何正确的解决方案来完美地做到这一点。
长期运行后,我决定进行以下操作,它正在按我的预期工作。
DispatchQueue.main.async {
// now update UI on main thread
self.myTableView.reloadData()
self.myTableView.setContentOffset(CGPoint(x: 0, y: -1), animated: true)
self.view.activityStopAnimating()
}
感谢您的所有答复。.我正在等待完美的答复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.