【问题标题】:Images in UIPageViewController are repeating in UICollectionView on every time cell creates. Using library for displaying Images in scrollView每次创建单元格时,UIPageViewController 中的图像都会在 UICollectionView 中重复。使用库在滚动视图中显示图像
【发布时间】:2019-06-03 21:53:07
【问题描述】:

我有一个关于我用于在UIPageViewController 中显示图像的库的具体问题,我在UICollectionViewthis is the library 中添加了我正在使用的库,我所有的工作都是通过这个库首先完成的@987654326 @,但是当我向上和向下滚动 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) }
}

现在我想我知道问题是这个库只有 append 函数,但是这个函数不会在创建新对象时删除它的值,我已经尝试修复它但我无法实现它,因此,如果我能在这里获得任何帮助,我将不胜感激。 有关了解我的问题的更多详细信息,请在下面分享 gif。

【问题讨论】:

    标签: ios swift uicollectionviewcell uipageviewcontroller reuseidentifier


    【解决方案1】:

    我不太确定,但我认为您的问题出在

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

    【讨论】:

    • 是的,我也试过这个,但它只是不断地将单元格的照片转移到任何其他单元格。我认为我需要从它附加的某个地方删除价值,这是我没有得到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多