首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速滚动过程中的UITableView冻结

快速滚动过程中的UITableView冻结
EN

Stack Overflow用户
提问于 2016-05-13 20:00:54
回答 1查看 5.5K关注 0票数 2

我大概有3-4周的问题。我搜索了,检查了所有的东西,但还是没有用。请帮帮我!

在每一个移动的卷轴上,cellForRowAtIndexPath重新加载tableView,因此,它开始冻结。

我的cellForRowAtIndexPath函数的表视图如下:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as! MoviesTVC
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
        let dictionary = self.rows[indexPath.row] as? [String: AnyObject]
        dispatch_async(dispatch_get_main_queue(),{
            cell.setCell(dictionary!)
        })

    })

    return cell
}

setCell()函数:

代码语言:javascript
复制
func setCell(dictionary: AnyObject){
    let ImgString = dictionary["src"] as? String;
    let ImgUrl = NSURL(string: ImgString!);
    let ImgData = NSData(contentsOfURL: ImgUrl!)
    self.movImg.image = UIImage(data: ImgData!);
    self.movName.text = dictionary["name"] as? String;
    self.movComment.text = dictionary["caption"] as? String;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-13 20:11:26

在后台异步任务中有错误的代码位。目前,您只从后台的数组中获得一个值,这是一个非常快速的过程.

您应该做的是在后台运行困难的任务,然后在前台更新UI。

代码语言:javascript
复制
let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as! MoviesTVC
let dictionary = self.rows[indexPath.row] as? [String: AnyObject]
cell.setCell(dictionary!)

return cell


func setCell(dictionary: AnyObject){
    let ImgString = dictionary["src"] as? String;
    let ImgUrl = NSURL(string: ImgString!);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
        let ImgData = NSData(contentsOfURL: ImgUrl!)
        let image = UIImage(data: ImgData!);
        //Possibly resize the image here in the background task
        //so that the cpu doesn't need to scale it in the UI thread
        dispatch_async(dispatch_get_main_queue(),{
            self.movImg.image = image
        })
    })
    self.movName.text = dictionary["name"] as? String;
    self.movComment.text = dictionary["caption"] as? String;
}

编辑:在评论中回答你的问题。最简单的解决方案是为每个单元格添加一个属性,即“图像”。然后,当您加载单元格时,如果字典的" image“属性存在,则只需将该图像加载到单元格中即可。如果它不存在,然后下载并保存到字典中,然后将它添加到您的单元格中。

更困难的解决方案是将图像下载到本地资源位置。然后使用imageNamed从文件中加载图像。这将为您处理缓存和内存释放。这将是更好的选择。

更好的做法是使用CoreData。在任何这些解决方案中,当您运行不足时,您将不得不管理清除文件存储。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37218463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档