Reputation: 11782
I have an array with 10 entries. I want to display them on UICollectionView as 3 in each row so they will be like
A B C
D E F
G H I
J
Now I am using following code
#pragma mark - UICollectionView Datasource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return 3;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return [self.btnArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
Buttons *btn = [self.btnArray objectAtIndex:(indexPath.section * indexPath.row)];
NSURL* aURL = [NSURL URLWithString:btn.imagePath];
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:data]];
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
// 1
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize retval = CGSizeMake(75, 75);
return retval;
}
// 3
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(50, 20, 50, 20);
}
However When I hit
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
It gives me error
NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
what is wrong?
Upvotes: 1
Views: 1152
Reputation: 9544
As the error says, you need to register a nib or class for cells with that identifier. You do this once when you setup your view.
See the UICollectionView Documentation for these two methods:
– registerClass:forCellWithReuseIdentifier:
– registerNib:forCellWithReuseIdentifier:
Upvotes: 3