Ios UITableView在swift中滚动期间冻结

Ios UITableView在swift中滚动期间冻结,ios,swift,uitableview,Ios,Swift,Uitableview,我有这个问题大约3-4周。我在谷歌上搜索,检查了所有东西,但仍然没有工作。请帮帮我 在每个移动的滚动条cellforrowatinexpath上重新加载tableView因此,它开始冻结 cellforrowatinexpath函数的表视图如下所示: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell =

我有这个问题大约3-4周。我在谷歌上搜索,检查了所有东西,但仍然没有工作。请帮帮我

在每个移动的滚动条
cellforrowatinexpath
上重新加载
tableView
因此,它开始冻结

cellforrowatinexpath
函数的表视图如下所示:

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()
函数:

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;
}

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

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

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”单元格的字典添加一个属性。然后,在加载单元格时,如果字典的“image”属性存在,则可以将该图像加载到单元格中。如果它不存在,则下载并保存到字典中,然后将其添加到您的手机中

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


更好的方法是使用CoreData。在任何一种解决方案中,当文件存储不足时,您都必须管理清除文件存储。

谢谢!冻结问题解决了,但在这种情况下,图像下载在每个移动的滚动。我的意思是,在滚动期间调用setCell()函数。您有什么建议吗?@ceyhunahurbeyli-您通常希望在滚动时启动图像请求(例如,您向下滚动以显示另外三行,因此您确实希望立即请求这些图像)。诀窍在于,您希望停止对已滚动到屏幕外但尚未完成图像下载的单元格的请求。这意味着不使用NSData(contentsOfURL:)。有很多不错的
UIImageView
扩展,可以优雅地处理这些东西,让您摆脱困境。@Putz1103非常感谢!我现在要检查第一个建议。除了Putz1103下面的观察之外,异步更新单元格的过程比您的代码(或他的代码)想象的要复杂得多。你应该考虑(a)细胞被重复使用的时间;(b) 快速滚动,可见单元格图像请求被积压在已滚动出视图的待处理单元格请求之后;查看所有这些都是非平凡的,你可以考虑一个<代码> UIImage < /Cord>扩展,它可以更优雅地处理异步图像检索。请参阅。您可能应该检查单元格在异步调用后是否仍然对应于给定的索引路径(可能已滚动到屏幕外)。另外,避免使用!接线员,也许你应该用“如果让”