【发布时间】:2015-12-30 11:23:24
【问题描述】:
我是 swift 新手,不知道为什么 UITableView 没有显示来自 JSON 数组的动态数据。
我想通过使用swiftyJSON从iTunes中获取顶级应用列表,然后根据解析的数据创建一个动态表。
我的问题是,当我运行这段代码时,我得到的所有代码都是 5 行,其中的值相同,如下图所示
我怎样才能让它变得动态,我在哪里弄错了? 提前致谢。
更新代码:
import UIKit
struct Apps {
var name : String!
}
class createTable: UITableViewController {
var tableData = [Apps]()
//Mark Properties
@IBOutlet var appTableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Get the #1 app name from iTunes and SwiftyJSON
DataManager.getTopAppsDataFromItunesWithSuccess { (iTunesData) -> Void in
let json = JSON(data: iTunesData)
if let appArray = json["feed"]["entry"].array {
for appDict in appArray {
let appName : String! = appDict["im:name"]["label"].string
let ap = Apps(name: appName)
self.tableData.append(ap)
}
}
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Table view cells are reused and should be dequeued using a cell identifier.
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let rowData = self.tableData[indexPath.row]
cell.textLabel!.text = rowData.name
return cell
}
}
输出:
【问题讨论】:
-
你设置了数据源和委托吗?为什么在表格单元格行委托方法中调用
DataManager.getTopAppsDataFromItunesWithSuccess?试着在外面打电话。
标签: ios json swift uitableview swifty-json