2015-09-03 211 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)