gpt4 book ai didi

ios - 在 Swift 中使用 JSON 显示 UIActivityIndi​​catorView

转载 作者:行者123 更新时间:2023-11-29 02:04:05 25 4
gpt4 key购买 nike

我一直在尝试在应用程序启动时显示 UIActivityIndi​​catorView,直到 JSON 完成从网站获取数据然后隐藏它 (indicatorView)。但是我还是想不通!我正在使用 UITableView 来显示下载的数据。

let kivaLoadURL = "http://api.kivaws.org/v1/loans/newest.json"
var loans = [Loan]()

override func viewDidLoad() {
super.viewDidLoad()

getLatestLoans()

}

func getLatestLoans() {
let request = NSURLRequest(URL: NSURL(string: kivaLoadURL)!)
let urlSession = NSURLSession.sharedSession()
let task = urlSession.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in

if error != nil {
println(error.localizedDescription)
}

self.loans = self.parseJsonData(data)

dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})

})

task.resume()
}

func parseJsonData(data: NSData) -> [Loan] {
var loans = [Loan]()
var error:NSError?

let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary

if error != nil {
println(error?.localizedDescription)
}

let jsonLoans = jsonResult?["loans"] as! [AnyObject]
for jsonLoan in jsonLoans {
let loan = Loan()
loan.name = jsonLoan["name"] as! String
loan.amount = jsonLoan["loan_amount"] as! Int
loan.use = jsonLoan["use"] as! String

let location = jsonLoan["location"] as! [String:AnyObject]
loan.country = location["country"] as! String

loans.append(loan)
println("\(loan.name), \(loan.amount), \(loan.use), \(loan.country)")
}

return loans
}

我使用以下代码来显示指示器,我尝试在 self.tableView.reloadData() 之后编写以停止动画指示器或隐藏警报,但它没有用,这里是代码:

var alert: UIAlertView = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel");
var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()
alert.show();

最佳答案

在顶部定义变量,例如,

var categories = [String:String]()
var alert: UIAlertView = UIAlertView()
let kivaLoadURL = "http://api.kivaws.org/v1/loans/newest.json"

在你想添加like的地方初始化

var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating()

alert = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")
alert.addSubview(loadingIndicator);
alert.show()

只需在归还贷款之前添加一行即可隐藏警报

func parseJsonData(data: NSData) -> [Loan] {
....
alert.dismissWithClickedButtonIndex(0, animated: false)
return loans
}

试试这个,可能对你有帮助。

关于ios - 在 Swift 中使用 JSON 显示 UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001741/

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