Scroll上的UICollectionViewCell内容问题

时间:2015-02-08 20:32:34

标签: ios uicollectionview uicollectionviewcell

我在cellForItemAtIndexPath内实现单元格内容只需添加UIImageView并在内部绘制和居中图像,直到我滚动集合视图看起来很棒!!!见图片!!!

// ...........cell content setup

            switch indexPath.row {

            case 0:
                cell.alpha = 0.0
            case 1:
                // ...........drawing singular on cell content view

                var cellImageView = UIImageView(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
                cellImageView.image = MyStyles.imageOfSingular(frame: CGRectMake(0, 0, 50.5, 50.5))
                cellImageView.contentMode = UIViewContentMode.Center
                cell.backgroundView = cellImageView

                // ...........

Befor i scroll collection After scrolling there is an image appearing on cell indexPath 3 你可以看到滚动后有一个图像出现在单元indexPath 3上我不需要它! 我不知道如何处理! SOS

最终图片看起来像这样

final pic

但滚动后看起来像这样

enter image description here 我以这种方式处理文本:

// ...........title setup and check if cellTitle exists to avoid title overlaping

        let cellTitle = UILabel(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))

        for cellTitle in cell.contentView.subviews {

            if (cellTitle.isKindOfClass(UILabel)) {

                cellTitle.removeFromSuperview()
            }
        }

但不知道如何处理图像!

1 个答案:

答案 0 :(得分:0)

我只是想通了! Label是contentView的子视图,但不是backgroundView!所以相反cell.contentView.subviews我只是在cell.subviews中循环,我的图像只是盯着它们的位置而不是因为可重复使用的单元而在滚动时填充其他单元格!

for cellImageView in cell.subviews {

            if (cellImageView.isKindOfClass(UIImageView)) {

                cellImageView.removeFromSuperview()
            }
        }