Reputation: 229
I'm working on a project right now and I'm using SwiftyJSON to parse the json from my server, at first it loads great, but when i reload it, it doesn't seem to work until i restart the app, here's the method that i used:
override func viewDidLoad() {
super.viewDidLoad()
connectToServer()
self.refreshControls.addTarget(self, action: "connectToServer", forControlEvents: .ValueChanged)
self.tableView.addSubview(self.refreshControls)
}
func connectToServer() {
let urlString = "http://myserver.com/"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
self.parseJSON(json)
}
}
}
func parseJSON(json: JSON) {
numberOfRows = json.count
for i in 0...numberOfRows {
let name = json[i]["name"].stringValue
let age = json[i]["age"].stringValue
let g = json[i]["gender"].stringValue
let b = json[i]["bday"].stringValue
names.append(name)
ages.append(age)
genders.append(g)
bdays.append(b)
}
tableView.reloadData()
self.refreshControls.endRefreshing()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return numberOfRows
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! MainCell!
if cell == nil {
cell = MainCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
}
cell.nameLbl.text = names[indexPath.row]
cell.ageLbl.text = ages[indexPath.row]
cell.genderLbl.text = genders[indexPath.row]
cell.bdayLbl.text = bdays[indexPath.row]
return cell
}
Did I missed something? Thanks!
Upvotes: 2
Views: 119
Reputation: 285059
Suggestion for a better workflow.
Create a custom class
class Person {
let name : String
let age : String
let gender : String
let birthday : String
init(name: String, age : String, gender: String, birthday : String)
{
self.name = name
self.age = age
self.gender = gender
self.birthday = birthday
}
}
In your view controller declare a variable people
var people = [Person]()
Change parseJSON
to this code for creating Person
instances
func parseJSON(json: JSON) {
people.removeAll()
for i in 0..<json.count {
let item = json[i]
let person = Person(name: item["name"].stringValue,
age: item["age"].stringValue,
gender: item["gender"].stringValue,
birthday: item["bday"].stringValue)
people.append(person)
}
tableView.reloadData()
self.refreshControls.endRefreshing()
}
Then return the number of persons in numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
and update the UI with
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! MainCell
if cell == nil {
cell = MainCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
}
let person = people[indexPath.row]
cell.nameLbl.text = person.name
cell.ageLbl.text = person.age
cell.genderLbl.text = person.gender
cell.bdayLbl.text = person.birthday
return cell
}
Upvotes: 3
Reputation: 229
I just figured it out. I just cleared all entities as vadian says. Like this:
func connectToServer() {
names.removeAll()
ages.removeAll()
genders.removeAll()
bdays.removeAll()
let urlString = "http://myserver.com/"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
self.parseJSON(json)
}
}
}
Thanks for all you comments!
Upvotes: 0