我有一个很大的问题!我从firebase中获取数据,将其保存到一个数组中,然后使用cellForRowAt填充该行。最后,我运行tableView.reload()。我必须运行tableView.reload(),因为cellForRowAt在数组填充db元素之前运行。
问题是,当我运行tableView.reload()时,应用程序冻结,如果我单击任何按钮,它都不会工作。该按钮仅在tableView.reload()完成运行时运行。
填充完数组后,我立即运行tableView.reload()。
下面是一些代码:
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!
}发布于 2020-10-07 23:29:19
问题出在使用
let imageData = try? Data(contentsOf: imageURL!)这阻塞了当前的主线程,不是因为tableView.reloadData(),您最好使用SDWebImage
https://stackoverflow.com/questions/64247291
复制相似问题