Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView cells are invisible

I recently added a UICollectionView to my storyboard, it's currently pushed into view by another view and this appears to be working fine however, using the storyboard editor i set the view to contain 35 cells which in the editor look fine, but when i run the app the cells are invisible. Some of the cells have UIButtons inside and these don't render either.

To double check the view was rendering i changed the background colour in the editor and ran it again, the colour updated correctly. Am i right in assuming that the setup should automatically render the cells without me having to run any delegate code if they have been setup in the editor?

Any help would be appreciated.

Thanks

EDIT----

Ok i have implemented the appropriate delegate methods in my second view controller that then pushes a segue to bring up the collectionview controller, i also added the to my second view controller header file and added the following code to the .M file:

// number of sections for the view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

// numbers of items in section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

// cell to return (uses the custom identifier of the cell in the storyboard)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"quickNoteCell" forIndexPath:indexPath];
    return cell;
}

I have also made sure the identifier in the cell on the storyboard uses the quickNoteCell, and changed its default colour to blue, however i am still not seeing the cell any ideas?

like image 273
SmokersCough Avatar asked Nov 06 '12 16:11

SmokersCough


2 Answers

You are not seeing any cells because you have generated a UICollectionViewController subclass, which added the following line in viewDidLoad:. From my understanding, this produces blank cells that do get compressed to size 0,0 when using autolayout.

Delete this line to see the cells that you defined in storyboard instead of invisible UICollectionViewCells

 // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
like image 101
Alex Stone Avatar answered Sep 27 '22 21:09

Alex Stone


Like Jay said, put the line here under your @interface and it should work. Nothing is wrong in the code you posted.

<UICollectionViewDelegate, UICollectionViewDataSource>
like image 41
Alex Avatar answered Sep 27 '22 21:09

Alex