gpt4 book ai didi

ios - 如何在我的 Collection View 中显示数据获取和显示之前的事件指示器

转载 作者:行者123 更新时间:2023-11-28 11:06:55 24 4
gpt4 key购买 nike

我有一个 Collection View 。我正在从 API 获取一些数据并在我的 Collection View 中显示它。一切正常。但是当我加载我的屏幕时 - 首先显示我的屏幕,只有在延迟 5 到 6 秒后,数据才会被填充在我的收藏 View 中。为了解决这个问题,我做了一些dispatch main thread来快速获取数据。

但有时根据用户手机的数据连接,数据会延迟显示。例如,如果用户的数据连接速度较慢,则需要大约 30 秒(假设)才能在我的 Collection View 中显示数据。

所以,我需要的是 - 如何显示事件指示器 - 直到我的数据显示在我的 Collection View 中。我知道如何创建一个虚拟的 Activity Indicator 并显示 1 到 30 秒。但我必须动态地执行此操作。

这意味着,我需要显示事件指示器,直到数据显示在我的 Collection View 中。它不应取决于用户的数据连接速度。

如何实现?

这是我的代码:

 var BTdata = [BTData]()
override func viewDidLoad()
{
super.viewDidLoad()

ListBusinessTypes()

}
// Values from Api for Business Types
func ListBusinessTypes()
{
let token = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String

let headers = ["x-access-token": token]

let request = NSMutableURLRequest(URL: NSURL(string: “some url“)!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil)
{
print(error)

let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert)

// add an action (button)
ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

// show the alert
self.presentViewController(ErrorAlert, animated: true, completion: nil)
}
else
{
if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject>
{
let success = json["success"] as? Int

if(success == 1)
{

if let typeValues = json["data"] as? [NSDictionary]
{
dispatch_async(dispatch_get_main_queue(),{

for item in typeValues
{
self.BTdata.append(BTData(json:item))
}

self.collectionView1!.reloadData()
})
}
}
else
{
let message = json["message"] as? String

let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)

// add an action (button)
ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

// show the alert
self.presentViewController(ServerAlert, animated: true, completion: nil)
}
}
}
})

dataTask.resume()
}

最佳答案

我对你的代码做了一些修改,下面是

将它们用作类变量

var actView: UIView = UIView()
var loadingView: UIView = UIView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
var titleLabel: UILabel = UILabel()

在你的服务调用函数中

showActivity(self.view, myTitle: "Loading...")
let dataTask = session.dataTaskWithRequest(request) {(data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
if response != nil {
if error != nil {
print(error)
removeActivity(self.view)
let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(ErrorAlert, animated: true, completion: nil)
}else {
if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject> {
let success = json["success"] as? Int
if(success == 1) {
if let typeValues = json["data"] as? [NSDictionary] {
dispatch_async(dispatch_get_main_queue(),{
for item in typeValues {
self.BTdata.append(BTData(json:item))
}
self.collectionView1!.reloadData()

removeActivity(self.view)
})
} else {
removeActivity(self.view)
}
} else {

removeActivity(self.view)

let message = json["message"] as? String
let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(ServerAlert, animated: true, completion: nil)
}
} else {

removeActivity(self.view)
}
}
}
})

}

dataTask.resume()

开始动画的函数

  func showActivity(myView: UIView, myTitle: String) {
myView.userInteractionEnabled = false
myView.window?.userInteractionEnabled = false
myView.endEditing(true)
actView.frame = CGRectMake(0, 0, myView.frame.width, myView.frame.height)
actView.center = myView.center
actView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)

loadingView.frame = CGRectMake(0, 0, 80, 80)
loadingView.center = myView.center
loadingView.backgroundColor = THEME_COLOUR
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 15

activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);

titleLabel.frame = CGRectMake(5, loadingView.frame.height-20, loadingView.frame.width-10, 20)
titleLabel.textColor = UIColor.whiteColor()
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.text = myTitle
titleLabel.font = IH_DELEGATE.BoldAppFontOfSize(10)

loadingView.addSubview(activityIndicator)
actView.addSubview(loadingView)
loadingView.addSubview(titleLabel)
myView.addSubview(actView)
activityIndicator.startAnimating()
}

停止动画的函数

func removeActivity(myView: UIView) {
myView.userInteractionEnabled = true
myView.window?.userInteractionEnabled = true
activityIndicator.stopAnimating()
actView.removeFromSuperview()
}

编辑 忘了说

let THEME_COLOUR = UIColor (red:0.188, green:0.682, blue:0.886, alpha:1)

关于ios - 如何在我的 Collection View 中显示数据获取和显示之前的事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com