我有一个像instagram这样的应用程序,可以在tableView中显示照片。 当网络速度很慢时,图像会在错误的单元格中重复使用,并且当我快速滚动时,我的下载指示标签也会在错误的单元格中重复使用。
我尝试使用异步图片加载,但无法对其进行优化。
这是我的cellForRowAtIndexPath方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:WallTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as WallTableViewCell
if indexPath.section == timeLineData.count - 1 {
println("load more pics")
loadPost()
}
cell.tag = indexPath.row
// timeLineData is my data array
if timeLineData.count != 0 {
let userPost = timeLineData.objectAtIndex(indexPath.section) as PFObject
cell.commentButton.tag = indexPath.section
// check if image is in the cache
if let imagePostedCache: AnyObject = self.imageCache.objectForKey(userPost.objectId){
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? WallTableViewCell {
cell.imagePosted.image = imagePostedCache as? UIImage
cell.downloadProgressLabel.hidden = true
}
})
}
// if it is not in the cache get the image from Parse
else if let imagesPost:PFFile = userPost["imageFile"] as? PFFile {
cell.imagePosted.image = nil
cell.downloadProgressLabel.hidden = false
imagesPost.getDataInBackgroundWithBlock({ (imageData:NSData!, error :NSError!) -> Void in
if !(error != nil) {
let image:UIImage = UIImage(data: imageData)!
// add image to the cache
self.imageCache.setObject( image , forKey: userPost.objectId)
// display image in the cell
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? WallTableViewCell {
cell.imagePosted.image = image as UIImage
}
})
}
else {
println("error")
}
}, progressBlock: { (progressStatus :Int32) -> Void in
cell.downloadProgressLabel.text = "\(progressStatus) %"
if progressStatus == 100 {
cell.downloadProgressLabel.text = ""
}
})
}
// Define description
if cell.tag == indexPath.row {
cell.brandLabel.text = userPost["photoText"] as? String
}
}
return cell
}
这是我的自定义单元格:
import UIKit
import QuartzCore
class WallTableViewCell: UITableViewCell {
@IBOutlet var downloadProgressLabel: UILabel!
@IBOutlet var commentButton: UIButton!
@IBOutlet var imagePosted: UIImageView!
@IBOutlet var brandLabel: UILabel!
}
答案 0 :(得分:1)
<强> WallTableViewCell 强>
import UIKit
import QuartzCore
class WallTableViewCell: UITableViewCell
{
var isDownloadingInProgress
@IBOutlet var downloadProgressLabel: UILabel!
@IBOutlet var commentButton: UIButton!
@IBOutlet var imagePosted: UIImageView!
@IBOutlet var brandLabel: UILabel!
func updateCellWithUserPost(userPost: PFObject)
{
if let imagePostedCache: AnyObject = self.imageCache.objectForKey(userPost.objectId)
{
self.imagePosted.image = imagePostedCache as? UIImage
self.downloadProgressLabel.hidden = true
}
else if let imagesPost:PFFile = userPost["imageFile"] as? PFFile
{
self.imagePosted.image = nil
self.downloadProgressLabel.hidden = false
isDownloadingInProgress = true
imagesPost.getDataInBackgroundWithBlock({ (imageData:NSData!, error :NSError!) -> Void in
isDownloadingInProgress = false
if !(error != nil)
{
let image:UIImage = UIImage(data: imageData)!
self.imageCache.setObject( image , forKey: userPost.objectId)
self.imagePosted.image = image as UIImage
}
else
{
println("Error while downloading image")
}
}, progressBlock: { (progressStatus :Int32) -> Void in
self.downloadProgressLabel.text = "\(progressStatus) %"
if progressStatus == 100
{
cell.downloadProgressLabel.text = ""
}
})
}
}
}
cellForRowAtIndexPath方法
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:WallTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as WallTableViewCell
let userPost = timeLineData.objectAtIndex(indexPath.section) as PFObject
if (!cell.isDownloadingInProgress)
cell.updateCellWithUserPost(userPost)
return cell
}
注意:我不太了解Swift因为我是纯粹的Objective-C程序员。如果您对答案有任何疑问,请与我们联系
答案 1 :(得分:0)
尝试使用SDWebImages。它将正确处理您的应用程序的图像。