简体   繁体   中英

Swift: load data from Json in TableViewController

My problem is: I can't to show the data in the tableview. I would like to show the data from the json , but I don't know how to get the data and put in one array, after that show it in the tableview

this is the code:

class TableViewControllerNews: UITableViewController {

    var Table:NSArray = []

    override func viewDidLoad() {
        super.viewDidLoad()
        CallWebService()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 0
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        cell.textLabel?.text = "the cell"

        return cell
    }


    func CallWebService()
    {
        let UrlApi = "http://www.kaleidosblog.com/tutorial/tutorial.json"
        let Url = NSURL(string: UrlApi)
        let Session = NSURLSession.sharedSession()
        let Work = Session.dataTaskWithURL(Url!, completionHandler: { dataTask, response, error -> Void in
            if (error != nil)
            {
                println(error)
            }
            var datos:NSData = NSData(data: dataTask)
            println(datos)
            println(response)
            self.ParseoDataToJson(datos)
        })

        Work.resume()
    }

    func ParseoDataToJson(datos:NSData)
    {
        let JsonWithDatos:AnyObject! = NSJSONSerialization.JSONObjectWithData(datos, options: NSJSONReadingOptions.MutableContainers, error: nil)

        println(JsonWithDatos)

        self.Table = JsonWithDatos as! NSArray
        println(self.Table)
        println(self.Table.count)
        //test
        var cell: AnyObject = self.Table[55]
        println(cell)

    }

为了在获取数据后将数据推送到UITableView,您需要调用self.tableView.reloadData()

You have got the data and putted in an array. Just need to load them into table view like this:

func ParseoDataToJson(datos:NSData)
{
    let JsonWithDatos:AnyObject! = NSJSONSerialization.JSONObjectWithData(datos, options: NSJSONReadingOptions.MutableContainers, error: nil)

    println(JsonWithDatos)

    self.Table = JsonWithDatos as! NSArray
    println(self.Table)
    println(self.Table.count)
    //test
    //var cell: AnyObject = self.Table[55]
    //println(cell)

    self.tableView.reloadData()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.Table.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    let item = self.Table[indexPath.row] as! [String : String]
    cell.textLabel?.text = item["country"]

    return cell
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM