1

私はiOS開発にかなり慣れていないので、優しくしてください。

プロジェクトにAlamofireおよびSwiftyJSONライブラリを使用して、サーバー上のJSONファイルを取得および解析しています。また、このチュートリアルを使用して、いくつかのセル「セル 1」「セル 2」「セル 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"]

エラーはなく、セルは元の値を保持しています。UITableView のセルを更新するための JSON 応答を取得するにはどうすればよいですか?

編集:

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

1 に答える 1

2

データの解析後にテーブルをリロードする必要があるため、次のようになります-

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()  <-----------------------
        }
}
于 2015-02-04T05:01:09.323 に答える