2016-01-06 41 views
2

我工作的一个项目,现在,我使用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 
    } 

难道我错过了什么?谢谢!

+0

的UI能否请您分享类定义和变量太decalaration(的tableView等)。 另外,如果调用tableView.reloadData()? –

+0

你检查你打'tableView.reladData()'和'被更新numberOfRows'? OT你应该制作一个模型并制作一个对象,而不是有四个不同的数组。 –

+1

你在哪里清除数据数组,如姓名,年龄......?刚刚追加它,numberOfRows得到了由Json.count更换,所以新增加的数据可能不会出现 – SolaWing

回答

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() 
} 

然后返回数的012人

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 
} 
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) 

      } 
     } 


    } 

谢谢你的意见!