我正在实施 KolodaView,网址为:https ://github.com/Yalantis/Koloda 。该viewForCardAt
函数返回一个UIView
,我UIView
将有一个需要下载的图像。问题是函数本身需要一个返回类型,UIView
但我无法知道该setupCard
方法的完成块何时执行完毕,因此我最终可能会返回一个空FlatCard
而不是FlatCard
在完成块中获得的。我尝试添加return a
到完成块,但这是不允许的。如何更改下面的代码以保证仅在执行完成块后才返回卡片。
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
var a = FlatCard()
if let listings = all_listings {
if index < listings.count {
setupCard(index: index, listings: listings, { (complete, card) in
if (complete) {
a = card
}
})
return a
}
}
return a
}
func setupCard(index: Int, listings : [Listing], _ completionHandler: @escaping (_ complete: Bool, _ card : FlatCard) -> ()) -> (){
let curr_card = FlatCard()
if let main_photo_url = listings[index].pic1url {
URLSession.shared.dataTask(with: main_photo_url, completionHandler: { (data, response, error) in
if (error != nil) {
print(error)
return
}
DispatchQueue.main.async {
curr_card.mainFlatImage = UIImage(data: data!)
}
})
completionHandler(true,curr_card)
return
} else {
completionHandler(true,curr_card)
return
}
}