SwiftyJSON 没有重新加载 UITableView

SwiftyJSON not reloading the UITableView

我现在正在做一个项目,我正在使用 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
    }

我错过了什么吗?谢谢!

我刚刚弄明白了。正如 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)

            }
        }


    }

感谢大家的评论!

改进工作流程的建议。

创建自定义 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
  }
}

在你的视图控制器中声明一个变量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()
}

那么return人数numberOfRowsInSection

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return people.count
}

并用

更新 UI
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
}