1

我已经实现了自定义 CollectionView,但是当我运行我的应用程序时它崩溃并显示, 错误:[UICollectionViewCell Mylabel]: unrecognized selector sent to instance...

这是我的代码片段,

- (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;
}

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

4

2 回答 2

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"];

也许这可以帮助你

于 2013-11-06T11:36:20.110 回答
1

试试这个

CollectionViewCell *cell = (CollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];
于 2013-11-06T11:26:38.973 回答