简体   繁体   中英

UICollectionView not working without storyboard

Hello I have created nib of UICollectionViewCell and set the identifier "gridcell". I am using following code but is throwing exception. Here is my code:

@implementation PhotosView
-(void)configureView:(id)object
{
    event=object;
   [collectionView registerClass:[PhotosCollectionViewCell class]
             forCellWithReuseIdentifier:@"gridcell"];

    if (photos.count<1) {

        NSString* url=[NSString stringWithFormat:@"%@getImage/%d.json",kBaseURL,[event.eventId intValue]];
        [self sendHttpPostJsonRequestWith:url jsonData:nil andRequestId:100];

    }
}

-(void)didReceiveData:(NSData *)data error:(NSError *)error andRequestId:(int)requestId
{
    if (data) {
        photos=[PhotosParser parseRoot:data];
        [collectionView reloadData];
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)aCollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosCollectionViewCell *cell=[aCollectionView dequeueReusableCellWithReuseIdentifier:@"gridcell" forIndexPath:indexPath];

    return cell;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 30;
}

@end

And the following is error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier gridcell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

You need to register your custom cell first in viewDidLoad

Like so:

    [self.yourCollectionView registerNib:[UINib nibWithNibName:@"PhotosCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"gridcell"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM