我有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中的圖像大小是固定的。
我會建議子類UITableViewCell並有一個cell.configureWithDictionary(cellDictionry)控制所有的信息將它從視圖控制器移開,之後,你是停止/繼續下載圖像?當圖像移出屏幕時,您不再需要「下載」它們排隊進行大量下載可能會減慢應用程序的速度,還會加載多少圖像?你可能會佔用大量的內存。 –
我不知道你的確切設置,所以我不能確定如何使它更順利,但是如果你有一些關於併發調用的使用NSURLSession,NSOperation和其他有用的類的wenderlich教程,如果你有時間我會閱讀這些。 –
阿洛斯,不要使用AFNetworking或SDWebImage,當你真的知道背景中發生了什麼時,它們很好用,但是如果你不這樣做,那麼你只是在安全套上鑽洞,仍然做愛會使工作完成大多數情況下,但是當發生不好的事情時,你無法控制它。 –