【问题标题】:SwiftyJSON: Update Cells in UITableView from JSON responseSwiftyJSON:从 JSON 响应更新 UITableView 中的单元格
【发布时间】:2015-04-03 12:27:23
【问题描述】:

我是 iOS 开发的新手,所以请保持温和。

我正在为一个项目使用AlamofireSwiftyJSON 库来获取和解析服务器上的JSON 文件。我还使用这个tutorial 来获得一个带有几个单元格“Cell 1”“Cell 2”“Cell 3”的 UITableView。

使用我编写的 SwiftyJson 插件:

request(.GET, url, parameters: parameters)
        .responseJSON { (req, res, json, error) in
            if(error != nil) {
                NSLog("Error: \(error)")               
                println(req)
                println(res)
            }
            else {
                NSLog("Success: \(url)")
                var json = JSON(json!) 

                var indexValue = 0           
                for (index, item) in enumerate(json) {
                    println(json[index]["Brand"]["Name"])

                    var indvItem = json[index]["Brand"]["Name"].stringValue

                    self.items.insert(indvItem, atIndex: indexValue)

                    indexValue++

                }

            }
    }

println() 行工作并显示正确的项目:

println(json[index]["Brand"]["Name"])

但是我似乎无法让他们进入牢房。

在文件的顶部,根据 UITableView 教程的说明,我有这一行:

var items = ["Cell 1", "Cell 2", "Cell 3"]

没有错误,单元格保持其原始值。 如何获取 JSON 响应以更新 UITableView 中的单元格?

编辑:

 @IBOutlet var tableView: UITableView!

var items = ["Cell 1", "Cell 2", "Cell 3"]


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel.text = self.items[indexPath.row]

    return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")
}

【问题讨论】:

  • 正常重载表格是这样做的,能不能显示表格查看数据源方法代码?
  • @rishi 添加了表格的所有代码。

标签: ios json uitableview alamofire swifty-json


【解决方案1】:

解析数据后需要重新加载表,所以像 -

request(.GET, url, parameters: parameters)
    .responseJSON { (req, res, json, error) in
        if(error != nil) {
            NSLog("Error: \(error)")               
            println(req)
            println(res)
        }
        else {
            NSLog("Success: \(url)")
            var json = JSON(json!) 

            var indexValue = 0           
            for (index, item) in enumerate(json) {
                println(json[index]["Brand"]["Name"])

                var indvItem = json[index]["Brand"]["Name"].stringValue
                self.items.insert(indvItem, atIndex: indexValue)

                indexValue++
            }
                self.tableView.reloadData()  <-----------------------
        }
}

【讨论】:

  • 确实有效 - 但它将项目添加到列表的开头。我怎样才能让它重写所有数据,使其只是表中的 JSON 响应?到目前为止,谢谢 - 我真的很感激。
  • 我不清楚你现在在问什么,你能添加一些问题的截图吗?我现在得到的你不希望你的预初始化数据,在这种情况下,在填充新数据之前,清理数组。
  • 接受了您的回答 - 抱歉让我等了几分钟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多