UICollectionView中的项目在滚动时消失

时间:2017-06-26 16:23:31

标签: ios uicollectionview

我有UICollectionViewController,它将以其自己的全屏模式以及另一个控制器的弹出视图显示。全屏模式完美运行。但是,在弹出模式下,所有项目在用户上下滚动内容几次(有时超过10次,但也会在第一次或第二次发生)后消失。

let vc = storyboard.instantiateViewController(
            withIdentifier: "myCollectionView") as? MyColle ctionViewController
// set up the overlap in the parent controller
let bounds = CGRect(x: x + 22, y: parentFrame.origin.y + dy,
                            width: width, height: parentFrame.height - dy - 10)
overlayView = UIView(frame: bounds)
overlayView!.layer.cornerRadius = 5.0
overlayView!.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.4)
self.view.addSubview(overlayView!)
vc.showAsPopup(overlayView!)

// in MyCollectionViewController, add itself to the parent popup view
func showAsPopup(_ parentView: UIView) {
    view.backgroundColor = UIColor.red // test color to show the view is never gone
    view.layer.cornerRadius = 5.0
    collectionView?.backgroundColor = UIColor.clear
    collectionView?.layer.cornerRadius = 5.0
    parentView.addSubview(view)
    view.frame = CGRect(x: 0, y: 20,
                         width: parentView.frame.width,
                         height: parentView.frame.height - 20)
    NSLog("my view bounds after: \(view.frame.origin.x) \(view.frame.origin.y)")
    NSLog("                    : \(view.frame.width)x\(view.frame.height)")
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView,
                             numberOfItemsInSection section: Int) -> Int {
    NSLog("\(items.count) items in section \(section)")
    return items.count
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenRect = view.bounds
    let scale = UIScreen.main.scale
    let width = screenRect.size.width - 20
    return CGSize(width: width, height: 45 * scale)
}

override func collectionView(_ collectionView: UICollectionView,
                             cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let item = items[indexPath.item]
    let cellId = "RCCell_\(item.id)"
    NSLog("creating cell \(cellId)")
    // Register cell classes
    self.collectionView!.register(RemoteControlViewCell.self,
                                  forCellWithReuseIdentifier: cellId)
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId,
        for: indexPath) as! RemoteControlViewCell

    // Configure the cell, override the icon in Cell
    cell.itemId = item.id
    cell.itemIcon.image = item.icon
    cell.itemDescriptionLabel.text = item.description

    return cell
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 12.5, left: 0, bottom: 12.5, right: 0)
}

override func collectionView(_ collectionView: UICollectionView,
                             didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    NSLog("removed cell at \(indexPath.row)")
}

// MyCollectionViewCell
override init(frame: CGRect) {
    super.init(frame: frame)

    let scale = UIScreen.main.scale
    NSLog("Cell frame w: \(frame.size.width) h: \(frame.size.height), scale \(scale)")

    let spacing = CGFloat(10)
    contentView.backgroundColor = UIColor.white
    contentView.layer.cornerRadius = 5
    contentView.layer.masksToBounds = true

    itemIcon = UIImageView(frame: CGRect(x: spacing, y: spacing,
                                           width: CGFloat(24.0), height: CGFloat(24.0)))
    itemIcon.contentMode = UIViewContentMode.scaleAspectFit
    contentView.addSubview(itemIcon)
    NSLog("icon w: \(itemIcon.frame.size.width)")

    let labelX = spacing + 5 + itemIcon.frame.size.width
    let font = UIFont.systemFont(ofSize: 20)
    itemDescriptionLabel = UILabel(
        frame: CGRect(x: labelX, y: spacing,
                      width: frame.size.width - labelX - spacing,
                      height: font.lineHeight))
    itemDescriptionLabel.textColor = UIColor(colorLiteralRed: 0x66 / 255.0, green: 0x66 / 255.0, blue: 0x66 / 255.0, alpha: 1.0)
    moduleDescriptionLabel.font = font
    contentView.addSubview(itemDescriptionLabel)
    NSLog("item desc \(String(describing: itemDescriptionLabel.text)) w: \(itemDescriptionLabel.frame.size.width)")
}

我尝试将collectionView的背景颜色更改为可见,并且可以看到即使项目消失,收集视图本身也不会消失。我还添加了对collectionView()回调方法的记录,发现它们在初始视图显示后再也没有被调用过。

什么可能导致项目在滚动期间消失?

1 个答案:

答案 0 :(得分:0)

尝试在prepareForReuse

中使用RemoteControlViewCell
override func prepareForReuse() {

      itemId = ""
      itemIcon.image = nil
      itemDescriptionLabel.text = ""

    }