gpt4 book ai didi

swift - 图像无法在表格 View 中显示

转载 作者:行者123 更新时间:2023-11-28 08:10:30 25 4
gpt4 key购买 nike

我创建了一个带有自定义单元格的 tableView,每个单元格都有一个图像。

在模型类中,我创建了一个 func mainPulatesData() 来使用 URLSession dataTask 方法从 url 中检索数据,并将数据转换为 UIImage 在完成处理程序 block 中,然后将图像添加到 UIImage 数组的变量中。

检索数据并将它们添加到 UIImage 数组的过程在 DispatchQueue.global(qos: .userInitiated).async block 中执行。根据打印消息,图像确实已添加到数组中。

但是,即使我在 tableView Controller 中创建了一个模型类的实例,并在 viewDidlLoad 中调用了 mainPulatesData(),图像也没有显示在表格中.

根据 TableView Controller 类中的其他打印消息,我发现它甚至可以添加到模型类中的数组中,但它似乎不适用于 tableView Controller 中的模型类实例。

这是模型类中获取图像数据的代码:

func mainPulatesData() {
let session = URLSession.shared
if myURLs.count > 0{
print("\(myURLs.count) urls")
for url in myURLs{
let task = session.dataTask(with: url, completionHandler: { (data,response, error) in
let imageData = data
DispatchQueue.global(qos: .userInitiated).async {
if imageData != nil{
if let image = UIImage(data: imageData!){
self.imageList.append(image)
print("\(self.imageList.count) images added.")
}
}
else{
print("nil")
}
}
})
task.resume()
}
}
}

这是在 View Controller 中创建模型实例的代码:

override func viewDidLoad() {
super.viewDidLoad()
myModel.mainPulatesURLs()
myModel.mainPulatesData()
loadImages()
}

private func loadImages(){
if myModel.imageList.count > 0{
tableView.reloadData()
}
else{
print("data nil")
}
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myModel.imageList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell
if myModel.imageList.count > 0{
let image = myModel.imageList[indexPath.row]
cell.tableImage = image
return cell

}
return cell
}

最佳答案

原因是在您 reloadData() 之后调用 cellForRowAt 时图像或 imageList 尚未准备好。

一个好的做法是在开始时使用占位符图像,并且仅在表格 View 单元格可见时才加载图像,而不是一次加载所有内容。像这样的东西:

// VC class
private var modelList = [MyModel(url: url1), MyModel(url: url2), ...]

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return modelList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell
cell.update(model: modelList[indexPath.row])
return cell
}

// Cell class
@IBOutlet weak var cellImageView: UIImageView!

func update(model: MyModel) {
model.fetchImage(callback: { image in
self.cellImageView.image = image
})
}

// Model class
final class MyModel: NSObject {
let url: URL
private var _imageCache: UIImage?

init(url: URL) {
self.url = url
}

func fetchImage(callback: @escaping (UIImage?) -> Void) {
if let img = self._imageCache {
callback(img)
return
}

let task = URLSession.shared.dataTask(with: self.url, completionHandler: { data, _, _ in
if let imageData = data, let img = UIImage(data: imageData) {
self._imageCache = img
callback(img)
} else {
callback(nil)
}
})
task.resume()
}
}

关于swift - 图像无法在表格 View 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43907996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com