找到你要的答案

Q:Show UIActivityIndicatorView with JSON in Swift

Q:表明在快速uiactivityindicatorview JSON

I've been trying to show the UIActivityIndicatorView on app launch until the JSON finish getting the data from the website and then hide it(indicatorView). But I still couldn't figure it out! I'm using UITableView to show the downloaded data.

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
    }

I used this following code to show the indicator, I tried to write after self.tableView.reloadData() to stop animating the indicator or hide alert but it didn't work, here is the code :

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

I've been trying to show the UIActivityIndicatorView on app launch until the JSON finish getting the data from the website and then hide it(indicatorView). But I still couldn't figure it out! I'm using UITableView to show the downloaded data.

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
    }

我用下面的代码显示的指示,我试图写后的自我。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();
answer1: 回答1:

Define variable on top like,

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

Initialise it where you want to add 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()

Just add line to hide alert before return loan

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

Try this, may help you.

在顶部定义变量,

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

只需添加行隐藏警报前归还贷款

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

试试这个,可以帮你。

ios  json  swift  uiactivityindicatorview