我大概有3-4周的问题。我搜索了,检查了所有的东西,但还是没有用。请帮帮我!
在每一个移动的卷轴上,cellForRowAtIndexPath重新加载tableView,因此,它开始冻结。
我的cellForRowAtIndexPath函数的表视图如下:
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;
}发布于 2016-05-13 20:11:26
在后台异步任务中有错误的代码位。目前,您只从后台的数组中获得一个值,这是一个非常快速的过程.
您应该做的是在后台运行困难的任务,然后在前台更新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“属性存在,则只需将该图像加载到单元格中即可。如果它不存在,然后下载并保存到字典中,然后将它添加到您的单元格中。
更困难的解决方案是将图像下载到本地资源位置。然后使用imageNamed从文件中加载图像。这将为您处理缓存和内存释放。这将是更好的选择。
更好的做法是使用CoreData。在任何这些解决方案中,当您运行不足时,您将不得不管理清除文件存储。
https://stackoverflow.com/questions/37218463
复制相似问题