2015-09-03 213 views
1

我有UITableView与每个单元格中的图像,我希望我的滚动顺利。所以,我读了一些stackerflow后,现在我加载在后台线程我的图片:UITableView平滑滚动

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: BuildingStatusCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BuildingStatusCell 
    cell.selectionStyle = UITableViewCellSelectionStyle.None 
    var node = nodesArray[indexPath.row] as! NSMutableDictionary 
    if !checkIfImagesLoaded(node[Api.pictures] as! NSMutableArray) { 
     cell.id = node[Api.buildingStatusId] as! Int 
     cell.date.text = node[Api.date] as? String 
     cell.count.text = String((node[Api.pictures] as! NSMutableArray).count) 
     cell.indicator.hidesWhenStopped = true 
     cell.indicator.startAnimating() 
     dbHelper.getBuildingStatusNode(node, callback: self) 
    } else { 
     cell.id = node[Api.buildingStatusId] as! Int 
     cell.date.text = node[Api.date] as? String 
     cell.count.text = String((node[Api.pictures] as! NSMutableArray).count) 
     dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) { 
      var image = WorkWithImage.loadImageFromSD((node[Api.pictures] as! NSMutableArray)[0]["image"] as! String)! // Bad 
      dispatch_async(dispatch_get_main_queue()) { 
       cell.imgView.image = image 
       cell.indicator.stopAnimating() 
      } 
     } 
    } 
    return cell 
} 

dbHelper.getBuildingStatusNode(节点,回调:个体经营)方法在后台线程执行也。但由于某些原因,当我滚动我仍然有一些延迟。我读过,用tableView:willDisplayCell方法代替tableView:cellForRowAtIndexPath填充我的单元格是非常好的,我应该尽可能以tableView:cellForRowAtIndexPath方法返回单元格。问题是我现在应该使用这样的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: BuildingStatusCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BuildingStatusCell 
    cell.selectionStyle = UITableViewCellSelectionStyle.None 
    return cell 
} 

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    var cell: BuildingStatusCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BuildingStatusCell 
    var node = nodesArray[indexPath.row] as! NSMutableDictionary 
    if !checkIfImagesLoaded(node[Api.pictures] as! NSMutableArray) { 
     cell.id = node[Api.buildingStatusId] as! Int 
     cell.date.text = node[Api.date] as? String 
     cell.count.text = String((node[Api.pictures] as! NSMutableArray).count) 
     cell.indicator.hidesWhenStopped = true 
     cell.indicator.startAnimating() 
     dbHelper.getBuildingStatusNode(node, callback: self) 
    } else { 
     cell.id = node[Api.buildingStatusId] as! Int 
     cell.date.text = node[Api.date] as? String 
     cell.count.text = String((node[Api.pictures] as! NSMutableArray).count) 
     dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) { 
      var image = WorkWithImage.loadImageFromSD((node[Api.pictures] as! NSMutableArray)[0]["image"] as! String)! 
      dispatch_async(dispatch_get_main_queue()) { 
       cell.imgView.image = image 
       cell.indicator.stopAnimating() 
      } 
     } 
    } 
} 

还有什么我可以做,让我的滚动更顺畅? BCS即使使用willDisplayCell方法,我仍然有滞后现象。

P.S.我的UITableViewCells中的图像大小是固定的。

+0

我会建议子类UITableViewCell并有一个cell.configureWithDictionary(cellDictionry)控制所有的信息将它从视图控制器移开,之后,你是停止/继续下载图像?当图像移出屏幕时,您不再需要“下载”它们排队进行大量下载可能会减慢应用程序的速度,还会加载多少图像?你可能会占用大量的内存。 –

+1

我不知道你的确切设置,所以我不能确定如何使它更顺利,但是如果你有一些关于并发调用的使用NSURLSession,NSOperation和其他有用的类的wenderlich教程,如果你有时间我会阅读这些。 –

+1

阿洛斯,不要使用AFNetworking或SDWebImage,当你真的知道背景中发生了什么时,它们很好用,但是如果你不这样做,那么你只是在安全套上钻洞,仍然做爱会使工作完成大多数情况下,但是当发生不好的事情时,你无法控制它。 –

回答

6

请尝试以下

  • 尝试删除任何阴影。
  • 使单元格及其子视图不透明。不要使用alpha /透明度。
  • 尝试在后台线程解码图像: 的所有最好是子类的UITableViewCell,只是通过你的API对象细胞,使细胞内这种映射Decode images in background thread?
1

第一。

另外它最好使用一些库:AFNetworking的扩展或AsyncImageView - 可以在Swift中使用。

尝试删除任何边框舍入,阴影,透明胶片 - 它们可能会导致延迟。在这种情况下,你需要光栅化:

相关问题: Sluggish scrolling experience when using QuartzCore to round corners on UIImageView's within a UITableViewCell

+0

只需要注意,不要使用某些库来做一些非常容易的事情并不是更好。请不要推荐那些不懂如何做事情的人去默认图书馆..尤其是当这些事情是相对简单的任务..... – TheCodingArt

-3

当您从URL加载图像是需要时间来下载图像和滚动UITableView这项事业块。 你正在做这么多工作根本就

  1. 使用这个类SDWebImage

  2. ,并在你的桥接头文件:

    #import "UIImageView+WebCache.h"

  3. 这里是一个代码示例,应该工作:

    let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in 
        println(self) 
    } 
    
    let url = NSURL(string: node[Api.pictures] as! NSMutableArray)[0]["image"] as! String) 
    
    cell.imgView.sd_setImageWithURL(url, completed: block)