[英]parse not loading first images in tableview
我正在使用解析来存储和检索一些数据,然后将这些数据加载到UITableview中,每个单元格包含一些文本和图像,但是当我打开tableview时,视图中的任何单元格都不会显示图像,除非将其滚动到视图之外并返回视图(我猜这是在调用cellForRowAtIndexPath)。 有没有一种方法可以检查何时下载了所有图像,然后重新加载tableview?
func loadData(){
self.data.removeAllObjects()
var query = PFQuery(className:"Tanks")
query.orderByAscending("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
for object in objects {
self.data.addObject(object)
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
self.cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as TankTableViewCell // let cell:TankTableViewCell
let item:PFObject = self.data.objectAtIndex(indexPath!.row) as PFObject
self.cell.productName.alpha = 1.0
self.cell.companyName.alpha = 1.0
self.cell.reviewTv.alpha = 1.0
self.rating = item.objectForKey("rating") as NSNumber
cell.productName.text = item.objectForKey("prodName") as? String
cell.companyName.text = item.objectForKey("compName") as? String
self.cell.reviewTv.text = item.objectForKey("review") as? String
let userImageFile = item.objectForKey("image") as PFFile
userImageFile.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
self.cell.productImage.image = image
}
}, progressBlock: {
(percentDone: CInt) -> Void in
if percentDone == 100{
}
})
self.setStars(self.rating)
// Configure the cell...
UIView.animateWithDuration(0.5, animations: {
self.cell.productName.alpha = 1.0
self.cell.companyName.alpha = 1.0
self.cell.reviewTv.alpha = 1.0
self.cell.reviewTv.scrollRangeToVisible(0)
})
return cell
}
您可以在UITableViewController中设置一个委托方法,该方法由另一个获取图像的控制器类调用。 我怀疑那是你想做的。
您应该执行的操作是使用默认图像初始化单元格,并使单元格控制器本身在后台获取图像,并在获取完成后更新其UIImageView。 您绝对不希望在重新加载表之前等待所有图像加载完毕,因为a。)需要很长时间,并且b。)如果一个失败或超时怎么办?
一旦单元加载了它的图像,如果它被回收器换出并getDataInBackground
,只要isDataAvailable
为true,则可以通过调用getData
而不是getDataInBackground
来简单地获取缓存的图像。
问题是您使用self.cell
并且每次返回单元格时self.cell
更改该引用。 因此,在加载图像时,所有图像都被设置到要返回的最后一个单元格中,该单元格可能不在屏幕上(或至少没有完全显示在屏幕上)。
确实,您应该在映像下载的完成块中捕获该单元格(并检查该单元格是否仍链接到相同的索引路径)。
另外,您应该缓存下载的图像,这样就不必总是下载数据/重新创建图像。
行后:
self.cell.productImage.image = image
尝试添加:
cell.layoutSubviews()
或self.cell.layoutSubviews()
它应在第一个表格视图上呈现子视图或在这种情况下的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.