使用 ImageSlideshow pod 时显示来自先前 tableview 单元格的重复图像
showing duplicated images from previous tableview cell when using ImageSlideshow pod
这是我的回复,我想在幻灯片视图中水平显示在表格视图中的图像是哪个媒体:
media": [
{
"id": 555,
"postId": 274,
"media": "https://onebusinessqrcode.s3.us-east-2.amazonaws.com/b885b600-2d2c-5d84-aa64-259e946763e9.png",
"createdAt": "2021-05-22T04:01:03.351Z",
"updatedAt": "2021-05-22T04:01:03.351Z"
},
{
"id": 554,
"postId": 274,
"media": "https://onebusinessqrcode.s3.us-east-2.amazonaws.com/81a71835-1808-5d16-b9ff-062a345a9612.png",
"createdAt": "2021-05-22T04:01:03.154Z",
"updatedAt": "2021-05-22T04:01:03.154Z"
}
]
但突然我发现我在同一个 tableview 行的视图中重复了这样的图像,而且我没有一个单元格的所有这些图片,你知道它可能来自可重复使用的单元格!可能来自数组我突然重复了它得到的图像!!:
滚动时单元格中的图像数量增加
我不知道为什么这是我的代码可能会对你有所帮助
// arr to store all images I got from server to show them
var postImages = [SDWebImageSource]()
在 tableView 的 cellForRowAt 中:
//Configure the cell...
let postText = ArraysModel.posts[indexPath.row]
if let pictureString = postText.media {
let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicCell
cell.postTextLabel.text = postText.postText
for image in pictureString {
let sources = SDWebImageSource(urlString: image.media ?? "")
if let sdImages = sources {
postImages.append(sdImages)
}
}
cell.slideShowImage.setImageInputs(postImages)
cell.slideShowImage.contentScaleMode = UIViewContentMode.scaleAspectFill
cell.slideShowImage.activityIndicator = DefaultActivityIndicator()
cell.slideShowImage.delegate = self
return cell
}
您可以将此添加到您的 PicCell class
override func prepareForReuse() {
super.prepareForReuse()
self.slideShowImage.setImageInputs([])
self.postImages.removeAll()
}
然后像这样改变你的 tableview 委托方法
let postText = ArraysModel.posts[indexPath.row]
if let pictureString = postText.media {
let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicCell
cell.postTextLabel.text = postText.postText
cell.prepareForReuse()
for image in pictureString {
let sources = SDWebImageSource(urlString: image.media ?? "")
if let sdImages = sources {
postImages.append(sdImages)
}
}
cell.slideShowImage.setImageInputs(postImages)
cell.slideShowImage.contentScaleMode = UIViewContentMode.scaleAspectFill
cell.slideShowImage.activityIndicator = DefaultActivityIndicator()
cell.slideShowImage.delegate = self
return cell
}
这是我的回复,我想在幻灯片视图中水平显示在表格视图中的图像是哪个媒体:
media": [
{
"id": 555,
"postId": 274,
"media": "https://onebusinessqrcode.s3.us-east-2.amazonaws.com/b885b600-2d2c-5d84-aa64-259e946763e9.png",
"createdAt": "2021-05-22T04:01:03.351Z",
"updatedAt": "2021-05-22T04:01:03.351Z"
},
{
"id": 554,
"postId": 274,
"media": "https://onebusinessqrcode.s3.us-east-2.amazonaws.com/81a71835-1808-5d16-b9ff-062a345a9612.png",
"createdAt": "2021-05-22T04:01:03.154Z",
"updatedAt": "2021-05-22T04:01:03.154Z"
}
]
但突然我发现我在同一个 tableview 行的视图中重复了这样的图像,而且我没有一个单元格的所有这些图片,你知道它可能来自可重复使用的单元格!可能来自数组我突然重复了它得到的图像!!:
滚动时单元格中的图像数量增加
我不知道为什么这是我的代码可能会对你有所帮助
// arr to store all images I got from server to show them
var postImages = [SDWebImageSource]()
在 tableView 的 cellForRowAt 中:
//Configure the cell...
let postText = ArraysModel.posts[indexPath.row]
if let pictureString = postText.media {
let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicCell
cell.postTextLabel.text = postText.postText
for image in pictureString {
let sources = SDWebImageSource(urlString: image.media ?? "")
if let sdImages = sources {
postImages.append(sdImages)
}
}
cell.slideShowImage.setImageInputs(postImages)
cell.slideShowImage.contentScaleMode = UIViewContentMode.scaleAspectFill
cell.slideShowImage.activityIndicator = DefaultActivityIndicator()
cell.slideShowImage.delegate = self
return cell
}
您可以将此添加到您的 PicCell class
override func prepareForReuse() {
super.prepareForReuse()
self.slideShowImage.setImageInputs([])
self.postImages.removeAll()
}
然后像这样改变你的 tableview 委托方法
let postText = ArraysModel.posts[indexPath.row]
if let pictureString = postText.media {
let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicCell
cell.postTextLabel.text = postText.postText
cell.prepareForReuse()
for image in pictureString {
let sources = SDWebImageSource(urlString: image.media ?? "")
if let sdImages = sources {
postImages.append(sdImages)
}
}
cell.slideShowImage.setImageInputs(postImages)
cell.slideShowImage.contentScaleMode = UIViewContentMode.scaleAspectFill
cell.slideShowImage.activityIndicator = DefaultActivityIndicator()
cell.slideShowImage.delegate = self
return cell
}