Correctly create uicollection view in iOS using Objective C

I've been trying to get a collection view working properly in iOS. I've been following this question: how to create custom UICollectionViewCell

I think I have most of it correct, I'm using the tutorials custom cell class exactly (see the link for the code for that) but I keep running into an error when I access the view controller with the collection view.

Here is my code:

#import "BackPackController.h"
#import "UICollectionViewCell+CollectionViewCell.h"

@interface BackPackController () <UICollectionViewDelegate, UICollectionViewDataSource>


@implementation BackPackController

NSArray *itemArray; //our item array

- (void)viewDidLoad {
    [super viewDidLoad];
    itemArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"PlayerItems"]; //get the players items from the user defaults and make an array of them
    _itemGrid.delegate = self;
    _itemGrid.dataSource = self;
    //[_itemGrid setDataSource:self];
    //[_itemGrid registerClass:CollectionViewCell.self forCellWithReuseIdentifier:@"cell1"];


- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSLog(@"Got this far");
    CollectionViewCell *cell1 = [_itemGrid dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    [cell1.customLabel setText:itemArray[indexPath.row]];
    return cell1;

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return itemArray.count;

- (CGSize)collectionView:(UICollectionView *)collectionView layout:   (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, 100);

I have correctly attached the controller as delegate and data source as far as I know. the NSLog in the code above does print its message so I know that cellForItemAtIndexPath method is being called. However as soon as it is called I am getting this error back and I am struggling to understand exactly what is causing it:

2023-11-16 08:36:19.163928+1300 Matoran Quest[7698:1793467] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:], UICollectionView.m:3389
2023-11-16 08:36:19.164525+1300 Matoran Quest[7698:1793467] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source returned nil from -collectionView:cellForItemAtIndexPath: for index path <NSIndexPath: 0x8db3e297ef8cf73e> {length = 2, path = 0 - 0}'

The list that is being accessed is a simple list of strings and I know that it is functioning correctly as I am accessing it elsewhere without problems. I've tried changing the text being added to the cell to just @"test" but this changes nothing


  • Ok, worked out why this was happening and its kind of dumb. I had a search bar attached to my collection view and I couldn't correctly allocate the view as an outlet because whenever I dragged my viewcontroller to my collectionview it kept trying to attach to the search bar instead of the collection view. of course I didn't have an outlet for the search bar so it kept showing up at nothing to attach and as a result because it wasn't attached the code wasn't working properly. I removed the search bar and attached it correctly then it worked fine. Thanks for the help though guys.