2017-06-26 68 views
1

我有一個UICollectionViewController,它將以其自己的全屏模式以及另一個控制器的彈出視圖顯示。全屏模式完美運行。然而,在彈出的模式下,所有項目用戶後消失滾動內容上下了幾次(有時超過10倍,而且還發生在第一或第二次)UICollectionView中的項目在滾動時消失

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()回調方法中,並發現它們在初始視圖呈現後從未再次被調用。

什麼可能導致項目在滾動過程中消失?

+0

PLZ使用prepareForReuse加'cellForItem'數據源代碼也 – Bali

+0

@Bali我增加了更多的代碼片斷。任何見解?謝謝 –

+0

您是否使用單元類 – Bali

回答

0

嘗試在RemoteControlViewCell

override func prepareForReuse() { 

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

    } 
+0

中的'PrepareForReuse'函數進行檢查否,這不起作用。 prepareForReuse()方法永遠不會被調用。 –