现在,在更新图像数据后,将在完成块中重新加载tableview。这个问题是,在更新每个单独的图像后,它似乎会重新加载,从而导致奇怪的行为,如闪烁和快速变化的图像。这是代码:
var knownForArray: [UIImage] = []
var knownForExtendedArray: [CastExtendedData] = []
override func viewDidLoad() {
CastExtendedData.updateAllData(urlExtension: "\(personID)/movie_credits", completionHandler: {results in
self.knownForData = results
if let knownFor = self.knownForData?.castExtended {
self.knownForExtendedArray = knownFor
for knownForImage in knownFor {
if let poster = knownForImage.poster {
self.updateImage(ImageType: DownloadPic.knownFor, ImageString: poster, completion: {_ in //This is the completion block where the tableview is reloaded
DispatchQueue.main.async {
self.knownForTableView.reloadData()
}
})
}
}
}
})
func updateImage (ImageType: DownloadPic, ImageString: String, completion: @escaping () -> Void) {
self.networkManager.downloadImage(imageExtension: "\(ImageString)", { (imageData) in
if let image = UIImage(data: imageData as Data) {
switch ImageType { //Was more case statements but edited out to make question more concise
case DownloadPic.knownFor: if let image = UIImage(data: imageData as Data){self.knownForArray.append(image)}
break
}
}
completion()
})
}
这是CellForRowAtIndexPath,它连接到一个带有嵌入式CollectionView的tableview单元格:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = knownForTableView.dequeueReusableCell(withIdentifier: knownForCellIdentifier) as! KnownForCell
cell.imageDelegete = self
cell.knownForExtendedArray = self.knownForExtendedArray
cell.knownForArray = self.knownForArray
DispatchQueue.main.async {
cell.knownForCollectionView.reloadData()
}
return cell
}
这是带有collectionView的tableviewCell:
var knownForArray: [UIImage] = []
var knownForExtendedArray: [CastExtendedData] = []
extension KnownForCell: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return knownForArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = knownForCollectionView.dequeueReusableCell(withReuseIdentifier: knownForReuseIdentifier, for: indexPath) as! KnownForCollectionViewCell
if let knownPoster = self.knownForExtendedArray[indexPath.row].poster{
DispatchQueue.main.async {
cell.knownForImages.sd_setImage(with: URL(string: "\(baseImageURL)\(knownPoster)"))//This is a cocoapod used to download images async (SDWebImage)
}
}
return cell
}
}