我工作的一个项目,现在,我使用SwiftyJSON解析从我的服务器的JSON,首先它加载很好,但是当我重新加载它,它似乎不工作直到我重新启动应用程序,下面是我使用的方法:SwiftyJSON不重装的UITableView
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
}
难道我错过了什么?谢谢!
的UI能否请您分享类定义和变量太decalaration(的tableView等)。 另外,如果调用tableView.reloadData()? –
你检查你打'tableView.reladData()'和'被更新numberOfRows'? OT你应该制作一个模型并制作一个对象,而不是有四个不同的数组。 –
你在哪里清除数据数组,如姓名,年龄......?刚刚追加它,numberOfRows得到了由Json.count更换,所以新增加的数据可能不会出现 – SolaWing