简体   繁体   中英

Images in UIPageViewController are repeating in UICollectionView on every time cell creates. Using library for displaying Images in scrollView

I have specific problem regarding a library I am using for displaying images in UIPageViewController which I have added in a UICollectionView , this is the library that I am using, all my work is done through this library on first viewDidLoad , but problem occurs when I scroll up and down the CollectionView, it gets keep repeating the images in that UIpageViewController , following is my piece of code where I am having issue.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell: FeedsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedsCollectionViewCell", for: indexPath) as! FeedsCollectionViewCell
            if postObject.contents.count > 0 {
                                for content in postObject.contents {
                cell.feedImageHeightConstaint.constant = 200
                cell.carouselViewHightContaint.constant = 200
                    let imageSlide = UIImageView()
                    let tap = UITapGestureRecognizer(target: self, action: #selector(showImage(_:)))
                    imageSlide.isUserInteractionEnabled = true
                    imageSlide.addGestureRecognizer(tap)
                    imageSlide.contentMode = UIView.ContentMode.scaleAspectFill
                    imageSlide.clipsToBounds = true
                    imageSlide.sd_setImage(with: URL(string: content.contentURL!), placeholderImage: nil) { [weak self](image, error, imageCacheType, url) in
                        guard let self = self else { return }
                        self.popUpFeedImage = image

                    }
                    cell.carouselView.appendContent(view: imageSlide) }
}

now i think i know that issue is this library has only append function but this function is not removing its values on new object creating, I have tried fixing it but I couldn't achieved it, so I am posting if here for any help that I can get would be appreciated. For more detail of understanding my issue, sharing a gif bellow.

图像重复

I'm not really sure, but i think your problem is in

cell.carouselView.appendContent(view: imageSlide)

just like you said, Also the loop, the image keep repeating itself because there's only one image. what your code is doing: is going through the loop and setting the values and when it finish it'll append the last one only. because that's what it's getting.

If i'm reading your code correctly try putting the append inside of the loop and it'll be like this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell: FeedsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedsCollectionViewCell", for: indexPath) as! FeedsCollectionViewCell
        if postObject.contents.count > 0 {
                            for content in postObject.contents {
            cell.feedImageHeightConstaint.constant = 200
            cell.carouselViewHightContaint.constant = 200
                let imageSlide = UIImageView()
                let tap = UITapGestureRecognizer(target: self, action: #selector(showImage(_:)))
                imageSlide.isUserInteractionEnabled = true
                imageSlide.addGestureRecognizer(tap)
                imageSlide.contentMode = UIView.ContentMode.scaleAspectFill
                imageSlide.clipsToBounds = true
                imageSlide.sd_setImage(with: URL(string: content.contentURL!), placeholderImage: nil) { [weak self](image, error, imageCacheType, url) in
                    guard let self = self else { return }
                    self.popUpFeedImage = image
                    cell.carouselView.appendContent(view: imageSlide)
                }
                     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM