我正在研究一个项目,我正在使用SwiftyJSON从我的服务器解析json,起初它加载很好,但是当我重新加载它时,它似乎不起作用,直到我重新启动应用程序,这是我使用的方法:
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
}
我错过了什么吗?谢谢!
答案 0 :(得分:3)
建议更好的工作流程。
创建自定义类
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
}
}
在视图控制器中声明变量people
var people = [Person]()
将parseJSON
更改为此代码以创建Person
个实例
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()
}
然后返回numberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
并使用
更新用户界面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
}
答案 1 :(得分:0)
我只是想通了。我刚刚清理了所有实体,如vadian所说。像这样:
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)
}
}
}
感谢您的所有评论!