首页 > 解决方案 > 使用 ImageSlideshow pod 时显示上一个 tableview 单元格中的重复图像

问题描述

这是我的回复,哪个媒体是我想在幻灯片视图中水平显示在 tableview 中的图像:


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
    }

标签: iosswiftiphonexcodeuitableview

解决方案


你可以在你的 PicCell 类中添加这个

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
}

推荐阅读