我想用服務器數據更新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