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

Show UIActivityIndicatorView with JSON in Swift

我一直在尝试在应用程序启动时显示 UIActivityIndicatorView,直到 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"

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

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()

只需在 return loan

之前添加一行以隐藏警报
func parseJsonData(data: NSData) -> [Loan] {
    ....
    alert.dismissWithClickedButtonIndex(0, animated: false)
    return loans
}

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