当我有对象的数组(其中包括必需的数据下载从谷歌商家API照片),当我尝试加载这些对象的元件中的tableView细胞,图像以及titleLabels在滚动时继续切换。UITableView的细胞图像保持对开关单元滚动
这里是的cellForRowAtIndexPath
我的tableView功能override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TestTableViewCell", forIndexPath: indexPath) as! TestTableViewCell
let business = businessObjects[indexPath.row]
if businessShown[indexPath.row] == false{
self.getImageFromPhotoReference(business.businessPhotoReference) { (photo, error) -> Void in
cell.backgroundImage.image = photo
}
}
cell.businessNameLabel.text = business.businessName
businessShown[indexPath.row] = true
return cell
}
func getImageFromPhotoReference(photoReference: String, completion: (photo: UIImage, error: NSError?) -> Void){
let photoRequestParams = ["key":"##", "photoreference": photoReference, "maxheight": "800"]
let placePhotoURLString = self.googleClient.buildPlacePhotoURLString(photoRequestParams)
let photoRequestURL = NSURL(string: placePhotoURLString)!
self.getDataFromUrl(photoRequestURL) { (data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
guard let data = NSData(contentsOfURL: photoRequestURL) where error == nil else { return }
let imageFile = UIImage(data: data)!
completion(photo: imageFile, error: error)
})
}
}
private func getDataFromUrl(url: NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError?) -> Void)) {
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
completion(data: data, response: response, error: error)
}.resume()
}
谢谢
HanekeSwift是完美的背景图像!非常感谢你! – user3796209