如何在重新加载时清除UICollectionViewCell?

zar*_*don 6 reload ios uicollectionviewcell swift

我有一个问题,即UICollectionView中显示的数据会覆盖标签,并且单元格视图不会被清除.

此图显示了问题,

IE:

UICollectionViewCell数据未被清除

我的UICollectionViewCell是这样构建的;

// in viewDidLoad
self.playerHUDCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier:reuseIdentifer)

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifer, for: indexPath) as UICollectionViewCell

        let arr = UINib(nibName: "EYPlayerHUDView", bundle: nil).instantiate(withOwner: nil, options: nil)
        let view = arr[0] as! EYPlayerHUDView
        cell.contentView.addSubview(view)

        if let allPlayers = self.allPlayers
        {
            let player:EYPlayer = allPlayers[indexPath.row]
            view.updatePlayerHUD(player: player)
        }

        cell.layoutIfNeeded()

        return cell
    }
Run Code Online (Sandbox Code Playgroud)

我使用视图在单元格中显示.

我尝试删除所有单元格中的子进程,cellForItemAt但它似乎删除了所有子视图.

我想知道如何清除UICollectionViewCell,因此UICollectionViewCell上的标签和其他信息不像上面的例子那样脏.

非常感谢

car*_*ine 12

在自定义单元类中使用prepareForReuse方法,如下所示:

override func prepareForReuse() {
    super.prepareForReuse()
    //hide or reset anything you want hereafter, for example
    label.isHidden = true

}
Run Code Online (Sandbox Code Playgroud)

在您的cellForItemAtIndexPath中,实例化您的自定义单元格:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellIdentifier", for: indexPath) as! CustomViewCell
Run Code Online (Sandbox Code Playgroud)

然后,始终在cellForItemAtIndexPath中,设置项目可见性/值