Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView - Not displaying cells

What I'm trying to achieve is a 3x3 table with UICollectionView. But it just doesn't display any cells when running the application. It does display the background color of the CollectionView though (which I set to blue) Can someone help me here?

What I have done is create a storyboard and simply dragged a CollectionViewController onto it. This controller also brought a nested CollectionView with it. Then I simply used the settings to make its background blue and to add a cell to it of which I made the background purple (both only for testing). Finally dragged a UILabel onto it. Now when run it just displays the blue background but no cells even though they are clearly visible in the editor. I've then tried to create a custom UICollectionViewController, assign the class to the CollectionViewController instead of the standard class and did the same to the cell (assign a custom UICollectionViewCell). In the custom UICollectionViewController I implemented the following methods:

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    AboutYourGoalsMultiSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [[cell goal]setText:@"T"];

    return cell;
}

I set the identifier of the cell to "Cell" and made an outlet connection of dataSource and delegate between the CollectionView and the custom UICollectionViewController. Everything compiles fine. So what am I missing? Shouldn't it have displayed something way before doing all these tweaks by simply using the editor?

The storyboard editor with all the changes I did

like image 965
Ke Vin Avatar asked Jan 17 '14 21:01

Ke Vin


4 Answers

See my answer here, in short, you need to delete this line if you are working with a storyboard:

 // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
like image 127
Alex Stone Avatar answered Nov 02 '22 04:11

Alex Stone


A common source of error is that dataSource and delegate are not set in the Outlets

like image 45
Patricia Avatar answered Nov 02 '22 06:11

Patricia


You have an error in the first line of your code.

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

NSIntegear should be NSInteger.

Not sure if that's the problem though because xcode shouldn't allow you to run your project with this error. I just tried following the exact same steps you mentioned (without the label) but with the above correction and it worked fine for me. Here is a screenshot:

enter image description here

like image 26
Rami Enbashi Avatar answered Nov 02 '22 04:11

Rami Enbashi


Since I didn't find a direct solution to it I've tried to set up a new project and did the same changes and tweaks and surprisingly, this time, everything worked from the beginning. I then copied the storyboard to my old project, deleted all the old files and replaced it with the files from the new project. Everything is working good so far. I have no idea what the actual error was but I'm happy it's working now. Thanks to everyone who had a look at my question!

like image 38
Ke Vin Avatar answered Nov 02 '22 06:11

Ke Vin