ios - 滚动水平 Collection View 时的随机行为

标签 ios swift uicollectionview

我有一个水平 Collection View ,它有 5 个带有标签的单元格和一个带有背景集的 View 。在最后一项中,我希望隐藏背景。所以我改成了这样。

    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    let cell = cell as! CartStepMenuCell
    let index = indexPath.item
    if index == 4{
        cell.seperator.isHidden = true
    }
}

问题是当来回滚动时,第一个单元格的背景被隐藏而最后一个不是。

最佳答案

collectionView 中的 View 会被重用,因此您对 View 所做的所有操作也必须撤消。我的规则是任何涉及可重用 View 的代码都不能有没有 elseif。在这种情况下,您可以完全删除 if:

    cell.seperator.isHidden = (index == 4)

通过这种方式,它可以在重复使用时正确设置和取消设置。

关于ios - 滚动水平 Collection View 时的随机行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460592/

相关文章:

iphone - 在没有导航 Controller 的情况下切换 View Controller

ios - FB ios SDK-3.0+-如何为[FBSession.activeSession openWithBehaviour:completedHandler]添加权限?

ios - 线程 1 : signal SIGABRT Login Button Causes App to Crash

ios - 带有 CollectionViewCell 和 UILongPressGesture 错误的 XIB

ios - Swift3 iOS - 如何在按下单元格之前从 TableView 中的所有 IndexPath 获取数据

ios - swift 2.2 循环的新语法

ios - UIScreen.mainScreen().bounds 始终为零

swift - 如何转换为类中的协议(protocol)类型?

ios - UICollectionView 插入/删除动画

ios - 访问 Assets 文件夹中的内部文件夹