簡體   English   中英

每次單元格創建時,UIPageViewController中的圖像都會在UICollectionView中重復。 使用庫在scrollView中顯示圖像

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

我有一個關於我使用在顯示圖像庫的具體問題UIPageViewController我在一個已經添加UICollectionView這是圖書館 ,我使用,我的工作是通過對第一這個庫做viewDidLoad ,但是當我滾動出現問題在CollectionView上下,它會不斷重復UIpageViewController的圖像,以下是我遇到問題的代碼。

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) }
}

現在我想我知道這個問題是這個庫只有追加函數但是這個函數沒有刪除它在新對象創建上的值,我試過修復它但我無法實現它,所以我發帖如果這里有任何幫助我能得到的將不勝感激。 有關理解我的問題的更多細節,請分享一個gif bellow。

圖像重復

我不太確定,但我認為你的問題在於

cell.carouselView.appendContent(view: imageSlide)

就像你說的那樣,循環,圖像不斷重復,因為只有一個圖像。 您的代碼正在做什么:正在循環並設置值,當它完成時它將僅附加最后一個。 因為這就是它的結果。

如果我正確地讀取你的代碼,請嘗試將append放在循環內部,它將是這樣的:

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)
                }
                     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM