【发布时间】:2013-05-31 21:56:38
【问题描述】:
我正在尝试让 UICollectionView 在我的应用程序中工作,但由于某种原因,我不断获得 sigabrt。我得到的错误是
* -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:] 中的断言失败
我用谷歌搜索了一下,似乎大多数时候这与忽略 registerClass forCellWithReuseIdentifier 有关,但我是按照视图加载方法中的要求执行此操作的。我的代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.collectionView reloadData];
self.refreshControl = [[UIRefreshControl alloc]init];
[self.collectionView addSubview:self.refreshControl];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell " forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
有人知道我可能会错过什么吗?
【问题讨论】:
-
错误信息中应该有另外一行来解释具体问题。尝试在 Xcode 调试器中单击 Continue,直到应用程序实际崩溃;您可能会在调试控制台中获得更多输出。
-
如果您正确复制了代码,那么您的单元格标识符不匹配。
-
哈哈,就是这样。单元格标识符中的错字!谢谢!
-
永远不要使用硬编码的标识符,如果只是为了避免这样的错误
标签: ios cocoa-touch uicollectionview