Anand
Anand

Reputation: 1129

UICollectionView with dynamic Cell Identifier

I want UICollection view to have dynamic CellIdentifier like follwoing.

    NSString *strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:strIdentifier forIndexPath:indexPath];

How can I do this? If its possible Please Help!!! Thanks

EDIT

I have registered all my identifiers with this code

    //CollectionView
    self.mpCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,  frame.size.height) collectionViewLayout:layout];
    [self.mpCollectionView setDataSource:self];
    [self.mpCollectionView setDelegate:self];
    for(int i=0;i<arrayExplorerItems.count;i++)
    {
        NSString* strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",i];
        NSLog(@"registered Id:%@",strIdentifier);
        [self.mpCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:strIdentifier];
    }

 and my cellForItemAtIndeaxPath is 

    UICollectionViewCell *cell;
    NSString *strIdentifier = [NSString stringWithFormat:@"cellIdentifier%d",indexPath.row];
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:strIdentifier forIndexPath:indexPath];

but giving me this error

could not dequeue a view of kind: UICollectionElementKindCell with identifier cellIdentifier0 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

Upvotes: 1

Views: 1049

Answers (1)

Juraj Antas
Juraj Antas

Reputation: 2712

It is possible. You need to register all cell identifiers first.

[collectionView registerNib:forCellWithReuseIdentifier:]

or

[collectionView registerClass:forCellWithReuseIdentifier:]

Then you can create and dequeue all prepared identifiers without trouble.

Upvotes: 1

Related Questions