【发布时间】:2017-07-24 14:03:10
【问题描述】:
那里!我需要你的帮助。我在 UICollectionView 中有 UIImage,它位于 UITableView 中。当我第一次从 API 获取数据时,它会正确显示图像,但是当我开始向下滚动并返回时,它会显示错误的图像。我的代码如下所示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
return cell
}
我的模型如下所示:
class UserContent {
var name = ""
var imageLinks = [UserImages]()
init(name: String, imageLinks: [UserImages]?) {
self.name = name
if imageLinks != nil {
self.imageLinks = imageLinks!
}
}
init() { }
deinit {
imageLinks.removeAll()
}
}
class UserImages {
var imageLink: String?
init(imageLink: String?) {
self.imageLink = imageLink
}
init() { }
deinit {
imageLink = ""
}
}
我在 UITableView 中做什么
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let index = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AllPostsOfUserTableViewCell
cell.collectionView.tag = index
let post = allPostsOfUserArray[index]
if post.imageLinks.count == 0 {
cell.collectionView.isHidden = true
} else {
cell.collectionView.isHidden = false
}
return cell
}
UPD: 我将 cell.collectionView.reloadData() 添加到 func tableView(cellForRowAt indexPath)。现在它工作正常。
【问题讨论】:
-
在您的所有
else(您没有这样做)中,您需要删除图像或放置一个占位符。
标签: ios swift uitableview uicollectionview