[英]swift: Completion handler
So, I have method loadData()
which download datas from parse.com 所以,我有方法
loadData()
从parse.com下载数据
And I should present all images show in table view. 我应该展示所有以表格视图显示的图像。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ReusableCell", forIndexPath: indexPath) as! LeaguesTableViewCell
loadData { (success) in
if success {
cell.leagueImage.image = UIImage(data: self.leaguesImage[indexPath.row])
cell.leagueNameLabel.text = self.leagues[indexPath.row]["name"] as? String
} else {
cell.leagueNameLabel.text = "Wait"
}
}
return cell
}
Its didn't work. 它没有用。 I call my function in
viewDidLoad()
but its not correct too, table view is empty. 我在
viewDidLoad()
调用函数,但它也不正确,表视图为空。
Cuz my array is empty
因为我的数组是空的
My 我的
The basic procedure for loading data into a UITableView is: 将数据加载到UITableView的基本过程是:
numberOfSectionsInTableView:
method: In your case there is only 1 section. numberOfSectionsInTableView:
的节数numberOfSectionsInTableView:
方法:在您的情况下,只有1个节。 tableView:numberOfRowsInSection:
: In your case return the number of leagues if the data is loaded. tableView:numberOfRowsInSection:
的行数:在您的情况下,如果已加载数据,则返回联赛数。 If the data is not loaded then return 1 so that the table view has at least one row to display the "Wait" message. leagues
and leaguesImage
. leagues
和leaguesImage
。 Example: 例:
private var loaded = false
override func viewDidLoad() {
super.viewDidLoad()
loaded = false
loadData() { success in
NSOperationQueue.mainQueue().addOperationWithBlock() {
self.loaded = success
self.tableView.reloadData()
}
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
if loaded {
return leagues.count
}
else {
return 1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ReusableCell", forIndexPath: indexPath) as! LeaguesTableViewCell
if loaded {
cell.leagueImage.image = UIImage(data: self.leaguesImage[indexPath.row])
cell.leagueNameLabel.text = self.leagues[indexPath.row]["name"] as? String
}
else {
cell.leagueNameLabel.text = "Wait"
}
return cell
}
Try to set delegate and datasource first. 尝试先设置委托和数据源。 If you have separate datasource other than view controller, retain it otherwise you will not get any callback.
如果您有视图控制器以外的其他数据源,请保留它,否则您将不会获得任何回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.