【问题标题】:CollectionView error: must pass a valid reuse identifierCollectionView 错误:必须传递有效的重用标识符
【发布时间】:2016-12-15 20:58:46
【问题描述】:

我正在尝试使用故事板中的 UICollectionView 原型单元,就像我们对 UITableView 所做的那样。但是collectionView.dequeueReusableCellWithReuseIdentifier(:, forIndexPath:) crash down 应用程序抛出错误

*** 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'必须通过有效的重用 -[UICollectionView 的标识符 dequeueReusableCellWithReuseIdentifier:forIndexPath:]'

UICollectionView 需要做什么?

【问题讨论】:

    标签: ios uicollectionview uistoryboard


    【解决方案1】:

    只需选择您的单元格 (CollectionViewCell) 并在 Attributes Inspector 中为其设置标识符名称:

    然后在代码中使用相同的标识符:

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath)
    

    【讨论】:

    • 这很奇怪,我刚刚在 ViewController 中测试了一个简单的实现,它被设置为 Collection View DataSource 和 Delegate。如果没有在 Attributes Inspector 中指定的标识符,应用程序会在您崩溃的方法中崩溃,但是当我设置标识符时,问题就解决了。如果您还没有,请记住在 Collection View DataSource 和 Delegate 上实现所有必需的方法。希望你能找到解决这个问题的方法。
    • 我第一次发现must pass a valid reuse identifier这个错误。
    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多