UICollectionView:发送到实例的无法识别的选择器

时间:2014-02-24 16:28:35

标签: ios objective-c xcode

我收到此错误。 *** 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;
 }

2 个答案:

答案 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