Ranch Ranch - 7 months ago 9
Swift Question

how to load in table cell? the following is json data

[{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}]


i have these type of json data, how could i load in table cell, what would be the key value for loading firstname and lastname?

Answer

Swift version

 //create a struct to represent your data
 struct People {
     var firstName: String!
     var lastName: String!
 }

 //create an array to store your data in your class
 var peopleArray: [People]?

 //when the view loads, take the data you receive and parse it
 for person in dataReceived {
     let personToAdd = People(firstName: person["firstName"], lastName: person["lastName"]
     peopleArray.append(personToAdd)
 }

 //cell for row at index path
 let cell = tableView.dequeueReusableCellWithIdentifier("identifier")
 let personForCell = peopleArray[indexPath.row]
 cell.textLabel!.text = personForCell.firstName
 cell.detailTextLabel.text = personForCell.lastName