【发布时间】:2015-04-03 12:27:23
【问题描述】:
我是 iOS 开发的新手,所以请保持温和。
我正在为一个项目使用Alamofire 和SwiftyJSON 库来获取和解析服务器上的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