ios - How to populate my tableView in Swift 2 from JSON? -
a new programmer here. how populate tableview json?
first problem json serialization , plugging in tableview.
code
import uikit class legislatorstablevc: uitableviewcontroller { // mark: variables & outlets private let cellidentifer = "cellreuse" // mark: view did load override func viewdidload() { super.viewdidload() // creating congfiguration object // session created // getting info/data let configuration = nsurlsessionconfiguration.defaultsessionconfiguration() let session = nsurlsession(configuration: configuration) let apikey = "https://congress.api.sunlightfoundation.com/legislators?apikey=xxxxxxxxxxxxxxxxxxxxx&all_legislators=true&per_page=all" if let url = nsurl(string: apikey) { // spawning task retrieve json data session.datataskwithurl(url, completionhandler: { (data, response, error) -> void in // checking error if let error = error { print("the error is: \(error)") return } // response if let httpresponse = response as? nshttpurlresponse httpresponse.statuscode == 200, let data = data { print("status code: \(httpresponse.statuscode)") // self.jsonserialization(data) } }).resume() } } // end of view did load // json serialization function swiftyjson.swift private func jsonserialization(data: nsdata){ // see gets status code 200 , i'm lost. { let json = try nsjsonserialization.jsonobjectwithdata(data, options: .mutablecontainers) as! [string: anyobject] } catch { print("error serializing json data: \(error)") } } // end of jsonserialization // mark: - table view data source // number of sections override func numberofsectionsintableview(tableview: uitableview) -> int { // #warning incomplete implementation, return number of sections return 1 } // end of number of sections // number of rows in section override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete implementation, return number of rows return 15 } // end of number of rows in section // cell row @ index path override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier(cellidentifer, forindexpath: indexpath) as! legislatortvcell // configure cell... cell.name.text = "name" cell.title.text = "title" cell.party.text = "party" return cell } // end of cell row @ index path }
create custom class
person
outside view controllerclass person { var firstname = "" var lastname = "" var title = "" var party = "" }
create array of
person
in view controllervar people = [person]()
the json has key
results
contains array of dictionaries.
inviewdidload
parse json , createperson
instances. reload table view.override func viewdidload() { super.viewdidload() // creating congfiguration object // session created // getting info/data let configuration = nsurlsessionconfiguration.defaultsessionconfiguration() let session = nsurlsession(configuration: configuration) let apikey = "https://congress.api.sunlightfoundation.com/legislators?apikey=xxxxxxxxxxxxxxxxxx&all_legislators=true&per_page=all" if let url = nsurl(string: apikey) { // spawning task retrieve json data session.datataskwithurl(url, completionhandler: { (data, response, error) -> void in // checking error if error != nil { print("the error is: \(error!)") return } else if let jsondata = data { { let parsedjson = try nsjsonserialization.jsonobjectwithdata(jsondata, options: []) as! [string:anyobject] guard let results = parsedjson["results"] as? [[string:anyobject]] else { return } result in results { let person = person() person.firstname = result["first_name"] as! string person.lastname = result["last_name"] as! string person.party = result["party"] as! string person.title = result["title"] as! string self.people.append(person) } dispatch_async(dispatch_get_main_queue()) { self.tableview.reloaddata() } } catch let error nserror { print(error) } } }).resume() } } // end of view did load
the table view delegate methods clear when using custom class.
sincecellforrowatindexpath
called code quite effective.override func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return people.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier(cellidentifer, forindexpath: indexpath) as! legislatortvcell let person = people[indexpath.row] cell.name.text = person.firstname + " " + person.lastname cell.title.text = person.title cell.party.text = person.party return cell } // end
of course couldn't test code might starting point.
Comments
Post a Comment