我想用服务器数据更新tableview。UITableView numberOfRows
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var aedList = [AED]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.aedList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in
self.aedList = JSONHelper().parseAEDInformation(json: response["data"])
dispatch_async(dispatch_get_main_queue(), {() -> Void in
cell.textLabel?.text = self.aedList[indexPath.row].street
self.tableView.reloadData()
})
}
return cell
}
正在正确下载数据,但tableview为空。 我想这是由于我的下载异步行为,并且numberOfRows
方法试图计数仍然是空的数组。
如何更正此问题才能正确显示数据?我是否也需要使用dispatch_async?
谢谢卢卡斯!我完全忘记了我正在从我的方法中为每个单元格的服务器加载内容的事实! – sesc360
它发生:),我很高兴我帮助 – Lukas
'numberOfSectionInTableView'是可选的,你不需要实现它。它默认返回1. – Sulthan