UITableView not updating after NSArray changed

前端 未结 1 781
春和景丽
春和景丽 2021-01-29 13:37

I have async request to HTTP, i want to read JSON and fill my viewtable.

I get data with request, i make NSArray, but i can pass it inside my functions, tableView number

相关标签:
1条回答
  • 2021-01-29 14:27

    You need to call reloadData() method after json data is downloaded:

    class ThirdView: UITableViewController {
        var jsonz: NSArray = ["Ray Wenderlich"]
        let url = NSURL(string: "http://iweddings.ru/xmlrestaurant.json")
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
                let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as! NSArray
                println(json)
    
                self.jsonz = json;
                self.tableView.reloadData()
            }
    
            task.resume()
        }
    
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1;
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            println(self.jsonz.count);
            return self.jsonz.count;
        }
    }
    
    0 讨论(0)
提交回复
热议问题