我有一个带3行的UICollectionViewController,在第一个indexPath(0)中我想添加一个UIView。当我启动应用程序时,它可以工作,但是当我进入另一个控制器并返回时,UIView在其他行中。这是代码:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let customCell = collectionView.dequeueReusableCellWithReuseIdentifier(customCellId, forIndexPath: indexPath) as! CustomCell
    customCell.nameLabel.text = "\(label2.text) \(materie[indexPath.row])"
    customCell.setupViews()
    if indexPath.row == 0 {
        customCell.setupUIView()
        customCell.nameLabel.text = ""
    }

    return customCell
}

当我启动应用程序时,屏幕是:
ios - 在UICollectionView中重新加载indexPath.row-LMLPHP
当我进入另一个控制器并返回时,屏幕是:
ios - 在UICollectionView中重新加载indexPath.row-LMLPHP

最佳答案

蓝色方块在另一个单元格中重复出现的原因是collectionView正在重用同一个单元格。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let customCell = ollectionView.dequeueReusableCellWithReuseIdentifier(customCellId, forIndexPath: indexPath) as! CustomCell

    // HERE YOU HAVE TO RESET THE CELL TO DEFAULT VALUES
    // LIKE REMOVING THE BLUE SQUARE VIEW
    customCell.view2.removeFromSuperview()

    if indexPath.row == 0 {
        customCell.setupUIView()
        customCell.nameLabel.text = ""
    } else {
        customCell.nameLabel.text = "\(label2.text) \(materie[indexPath.row])"
        customCell.setupViews()
    }

    return customCell
}

10-06 00:03