ios - UITableView 平滑滚动

标签 ios swift uitableview

我的 UITableView 在每个单元格中都有图像,我希望我的滚动流畅。所以我读了一些关于 stackerflow 的帖子,现在我在后台线程中加载我的图像:

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(node, callback: self) 方法也在后台线程中执行。但是由于某些原因,当我滚动时,我仍然会有一些延迟。我读到用 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 方法,我仍然有滞后。

附言我的 UITableViewCells 中的图像大小是固定的。

最佳答案

尝试以下操作

关于ios - UITableView 平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374212/

相关文章:

ios - Swift 上的 NavigationController 透明和背景图像

swift - 进程运行时 Mac Cocoa 应用程序 GUI 卡住

ios - 具有辅助功能的 UITableViewCell 中的 UICollectionView

ios - 为什么我的 tableview 直到第二次触摸才调用 didselectrowatindexpath?

ios - 同时推送ViewController

ios - Swift SideMenu 显示 View Controller

ios - 从字典中删除整个元素

ios - Swift 将视频上传到 Firebase 并在 UITableViewCell 中播放

java - 如何解决java.io.IOException : Read error when reading Excel file using apache POI

ios - 在关闭之前的 AlertViewController 之后呈现新的 AlertViewController - Swift