OGeek|极客世界-中国程序员成长平台

标题: ios - CollectionView 错误 : must pass a valid reuse identifier [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:13
标题: ios - CollectionView 错误 : must pass a valid reuse identifier

我正在尝试使用 Storyboard中的 UICollectionView 原型(prototype)单元,就像我们对 UITableView 所做的那样。但是 collectionView.dequeueReusableCellWithReuseIdentifier(:, forIndexPath 导致应用崩溃并抛出错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a valid reuse identifier to -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]'

UICollectionView 需要做什么?



Best Answer-推荐答案


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

enter image description here

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

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

关于ios - CollectionView 错误 : must pass a valid reuse identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38857082/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4