我收到此错误。 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell label]: unrecognized selector sent to instance 0x1eead660'
我正在使用nib文件作为我的单元格并尝试正确显示单元格。我猜我没有正确地返回细胞,但我不太确定。任何帮助将不胜感激。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSMutableArray *data = [sections objectAtIndex:indexPath.section];
cell.label.text = [data objectAtIndex:indexPath.item];
return cell;
}
答案 0 :(得分:10)
UICollectionViewCell没有名为label
的属性。
也许你的意思是:
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
假设您已将UICollectionViewCell
子类化,添加了label
,并调用了您的子类Cell
。
答案 1 :(得分:2)
您的nib文件可能需要以某种方式连接到您的自定义类('Cell'?)。完成后,你会打电话:
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
现在,您正在从UICollectionViewCell
获取一个香草dequeue...
对象,当您尝试将其用作Cell
时,您会遇到问题。
registerClass
代码不应该进入cellForItemAtIndexPath
。每个视图只需要调用一次,例如
static NSString *cellIdentifier = @"Cell";
@implementation YourCollectionViewController
// ...
- (void)viewDidLoad
{
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
}
// ...
@end