Uitableview 如何在TableViewCell内为collectionView异步下载图像?
我在tableViewCell中做了一个网格(collectionView),问题是每个单元格加载不同的图像。制作一个Json,如下所示:Uitableview 如何在TableViewCell内为collectionView异步下载图像?,uitableview,asynchronous,swift3,uicollectionview,Uitableview,Asynchronous,Swift3,Uicollectionview,我在tableViewCell中做了一个网格(collectionView),问题是每个单元格加载不同的图像。制作一个Json,如下所示: { { "name": "Vegetales" "images": { imagesURLStrings } }, { "name": "Frutas" "images": { imagesURLStrings } }, } 我使用自定义视图和异步下载 我认为问题在于,当我试图在tableview
{
{
"name": "Vegetales"
"images": { imagesURLStrings }
},
{
"name": "Frutas"
"images": { imagesURLStrings }
},
}
我使用自定义视图和异步下载
我认为问题在于,当我试图在tableviewCell中定义collectionView的单元格数量时,分配错误,无法工作,我不知道如何修复
下载图像的代码:
func loadImages() {
var section = 0
var row = 0
while (section < searchDataresults.count) {
for i in searchDataresults[section].images {
let key = section * 10 + row
let imageUrl = i
let url:URL! = URL(string: imageUrl)
task = session.downloadTask(with: url, completionHandler: { (location, response, error) -> Void in
if let data = try? Data(contentsOf: url){
// 4
DispatchQueue.main.async(execute: { () -> Void in
// 5
// Before we assign the image, check whether the current cell is visible
let img:UIImage! = UIImage(data: data)
saveImage(image: img, name: String(key))
})
}
})
task.resume()
row += 1
}
section += 1
row = 0
}
}
我真的很感谢你的帮助 子类UICollectionviewCell并重置集合视图单元格的内容
override func prepareForReuse() {
super.prepareForReuse()
self.customImageview.image = nil
}
您可以使用图像视图扩展,但该扩展已完成,但不起作用:'(@LeoDabus
override func prepareForReuse() {
super.prepareForReuse()
self.customImageview.image = nil
}