首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tableView.reload()在运行时冻结应用程序

tableView.reload()在运行时冻结应用程序
EN

Stack Overflow用户
提问于 2020-10-07 23:27:05
回答 1查看 30关注 0票数 0

我有一个很大的问题!我从firebase中获取数据,将其保存到一个数组中,然后使用cellForRowAt填充该行。最后,我运行tableView.reload()。我必须运行tableView.reload(),因为cellForRowAt在数组填充db元素之前运行。

问题是,当我运行tableView.reload()时,应用程序冻结,如果我单击任何按钮,它都不会工作。该按钮仅在tableView.reload()完成运行时运行。

填充完数组后,我立即运行tableView.reload()。

下面是一些代码:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageTableViewCell
        
        let preview2 = imageDownload(images[counter!]["previewURL"]! as! String)
        cell.img2.imagePreview = preview2
        cell.img2.setImage(preview2, for: .normal)
        cell.img2.addTarget(self, action: #selector(imgTapped(_:)), for: .touchUpInside)
        
        let preview1 = imageDownload(images[counter!-1]["previewURL"]! as! String)
        cell.img1.imagePreview = preview1
        cell.img1.setImage(preview1, for: .normal)
        cell.img1.addTarget(self, action: #selector(imgTapped(_:)), for: .touchUpInside)
        counter = counter! - 2
        
        return cell
    }

func imageDownload(_ url: String) -> UIImage {
     let imageURL = URL(string: url)
     let imageData = try? Data(contentsOf: imageURL!)
     let image = UIImage(data: imageData!)
     return image!
}

class ImageTableViewCell: UITableViewCell {
    @IBOutlet var img1: ExtendedButton!
    @IBOutlet var img2: ExtendedButton!
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-07 23:29:19

问题出在使用

代码语言:javascript
复制
let imageData = try? Data(contentsOf: imageURL!)

这阻塞了当前的主线程,不是因为tableView.reloadData(),您最好使用SDWebImage

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

https://stackoverflow.com/questions/64247291

复制
相关文章

相似问题

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