【问题标题】:SwiftyJSON not reloading the UITableViewSwiftyJSON 没有重新加载 UITableView
【发布时间】:2016-01-06 11:39:09
【问题描述】:

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

我错过了什么吗?谢谢!

【问题讨论】:

  • 能否请您也分享一下类定义和变量(tableView等)的声明。另外,如果 tableView.reloadData() 被调用?
  • 您是否检查过您点击了tableView.reladData() 并且numberOfRows 是否已更新? OT 你应该制作一个模型并制作一个对象,而不是有四个不同的数组。
  • 您在哪里清除了姓名、年龄等数据数组?你只是追加了它,numberOfRows 被 Json.count 替换了,所以新添加的数据可能不会出现
  • 不要为每个参数使用 4 个不同的数组。使用具有 4 个属性的自定义类作为参数,一个 数组作为实例,然后在 numberOfRowsInSection 中返回 array.count。此外,您必须在添加新条目之前删除所有条目。
  • numberOfRows 变量是一个 Int,我只是用它来声明它为numberOfRowsInSection。使用UIRefreshControl 拉下 tableView 时正在调用 tableView.reloadData()

标签: ios xcode swift uitableview swifty-json


【解决方案1】:

关于更好的工作流程的建议。

创建自定义类

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
}

并使用

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

【讨论】:

    【解决方案2】:

    我刚刚想通了。正如 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)
    
                }
            }
    
    
        }
    

    感谢你们所有的cmets!

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多