NSArray更改后,UITableView不更新 [英] UITableView not updating after NSArray changed

查看:65
本文介绍了NSArray更改后,UITableView不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对HTTP的异步请求,我想读取JSON并填充我的视图.

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

我通过请求获取数据,我制作了NSArray,但是我可以在函数内部传递它,tableView numberOfRowsInSection仅返回1,请帮忙.

I get data with request, i make NSArray, but i can pass it inside my functions, tableView numberOfRowsInSection return only 1, please help.

import Foundation

import UIKit

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)
// here i see in xCode output
//        price = 4500;
//        rating = 45;
//        slogan = "\U041d\U0435\U043b\U0435\U0433\U0430\U043b\U044c\U043d\U043e";
//        status = 1;
//        type = 1;
// etc....


            self.jsonz = json;
    }

    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;
// here i see always "1" ???? why?
}

NSArray jsonz 不变. 对不起,我的英语.

NSArray jsonz does not change. Sorry for my english.

推荐答案

在下载json数据后,您需要调用reloadData()方法:

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;
    }
}

这篇关于NSArray更改后,UITableView不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆