自定义CollectionView崩溃iOS

时间:2013-11-06 11:21:33

标签: ios iphone ipad uicollectionview uicollectionviewcell

我已经实现了Custom CollectionView,但是当我运行我的应用程序时,它会崩溃并显示, 错误:[UICollectionViewCell Mylabel]:无法识别的选择器发送到实例...

这是我的代码段,

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cvCell"];
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { // No of Rows..

    CGSize result = [[UIScreen mainScreen] bounds].size;

    if(result.height == 480) //3.5"
    {
        return 3;
    }
    else // 4"
    {
        return 4;
    }
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { // No of Columns..
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

   CollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CollectionViewCell" owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[CollectionViewCell class]])
            cell = (CollectionViewCell *)oneObject;
    }

    cell.Mylabel.text=@"I am label";
    cell.backgroundColor= [UIColor yellowColor];

    return cell;
}

我在哪里做错了?请提前帮助和感谢

2 个答案:

答案 0 :(得分:3)

我不确定你的问题,但我有类似的问题。

我从委托中删除了这一行:

if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CollectionViewCell" owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[CollectionViewCell class]])
            cell = (CollectionViewCell *)oneObject;
    }

我在View Did Load中添加了这个:

UINib *cellNib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cellID"];

可能这可以帮到你

答案 1 :(得分:1)

试试这个

CollectionViewCell *cell = (CollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];