简体   繁体   English

SwiftyJSON:从JSON响应更新UITableView中的单元格

[英]SwiftyJSON: Update Cells in UITableView from JSON response

I'm pretty new to iOS Development so please be gentle. 我是iOS开发的新手,请保持冷静。

I am using the Alamofire and SwiftyJSON libraries for a project to get and parse a JSON file that is on a server. 我将AlamofireSwiftyJSON库用于项目,以获取和解析服务器上的JSON文件。 I also have used this tutorial to get a UITableView with a few cells "Cell 1" "Cell 2" "Cell 3". 我还使用了本教程来获取带有几个单元格“ Cell 1”,“ Cell 2”,“ Cell 3”的UITableView。

Using the SwiftyJson plugin I have coded: 使用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++

                }

            }
    }

The println() line works and displays the correct items: println()行有效并显示正确的项目:

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

however I cannot seem to get them to go into the Cells. 但是我似乎无法让他们进入牢房。

At the top of the file, and from the instructions of the tutorial for the UITableView I have this line: 在文件的顶部,根据UITableView教程的说明,我有这行:

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

There are no errors, the cells just keep their original values. 没有错误,单元格仅保留其原始值。 How do I get the JSON response to update the Cells in the UITableView? 如何获取JSON响应以更新UITableView中的单元格?

Edit: 编辑:

 @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)!")
}

You need to reload table after parsing the data, so something like - 您需要在解析数据后重新加载表格,因此类似-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM