我有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,当你真的知道背景中发生了什么时,它们很好用,但是如果你不这样做,那么你只是在安全套上钻洞,仍然做爱会使工作完成大多数情况下,但是当发生不好的事情时,你无法控制它。 –