UICollectionView cellForItemAtIndexPath not registering cell UICollectionView cellForItemAtIndexPath not registering cell ios ios

UICollectionView cellForItemAtIndexPath not registering cell


If you just want to display an image, you don't need to do any subclassing, you can set the cell's backgroundColor with colorWithPatternImage:. Register the class like this:

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

Then use it like so:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];    return cell;}

In this example, results is an array of UIImages.


If you are using xib in applivation then add following in your viewdidLoad method

    [self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];

otherwise If you using storyboard add following

  [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];

Finally add this (If not)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath]; return cell;}

Hope above will help.


Try setting a breakpoint on

[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];

I would guess your loadView (did you mean viewDidLoad?) method is not being called, so the class is never registered with the collectionView.


matomo