Question

Basically I've been writing iOS apps for about 3 days (after reading a book to get the basic knowledge of design patterns and frameworks etc) and 1 entire day has been spent on this. I cannot get my CollectionView to display. I've got a storyboard that starts with a TabBarView and the two tabs (one is a TableView and the other is just a UIView) work, but the third tab (UICollectionView) just displays a black screen even after setting up. How I set it up:

1) dragged a ViewController to the storyboard

2) made a relationship segue between my UITabBarController to the new ViewController

3) dragged a UICollectionView to the new ViewController

4) dragged 4 UICollectionViewCell's to the CollectionView

5) dragged 4 UILabels into said CollectionViewCell's

6) made a connection between the new CollectionView's delegate & data source and the header file of my CollectionView class (this might be where I went wrong, I did not make the connection to my ViewController.h class as I used this one for my UITableView and thought a new class was necessary)

7) declared the following methods in my CollectionView.m file

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//Populates each cell using the data given (no data in this case).
//Should return a CollectionViewCell.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //Gets the number of cells in each section.
    //Should return an integer value.
    return 2;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    //Tells the collection view the number of sections it should have.
    //Should return an integer value.
    return 4;
}

I get the feeling there is something I missed out, as this logic makes sense to me as a programmer but, having such little experience in Xcode, maybe I forgot to link up this class to my CollectionView or something. Does anybody know where I went wrong?

BTW in case you're wondering, I'm using this CollectionView more of a navigation technique than to display wonderful images or anything, I am going to populate them with generic images later when they actually show up in the emulator. thanks

Was it helpful?

Solution

It is not displaying anything because default color of label is black, you'll have to change the color of text to any light color to see text over cell.

Now, 

->Add a new file which should be subclass of UICollectionViewCell, Ctrl+drag from label to this class.(for eg I took mainViewCell)and label property name I took labelText

->Declare reuseIdentifier for cell from Attribute Inspector,(mainCell, I took)

->Import cellClass to your UIViewController Class,

->Using this method to display any text

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    mainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCell" forIndexPath:indexPath];
    cell.labelText.text = @"What-ever-you-want-display";
    return cell;
}

hope this would help...

OTHER TIPS

Did you tell your view controller(the one that contains the collection view) to use UICollectionViewDelegateFlowLayout? For example,

@interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

Also, have you set the reuse identifier for your cell? In Interface Builder set this in the attributes inspector. Then go to the cellForItemAtIndexPath: method and change this line to use your identifier. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

Check out UICollectionViewDelegateFlowLayout in the documentation for methods used to size cells and set margins and spacing.

The step 6) might be inadequate. Have you ctrl-dragged the collectionView to the controller icon beneath the main view directly? You would have been prompted in a black dialog box for connecting dataSource and delegate. The connections are made in XCode this way. Alternatively you could explicitly set the dataSource as self in the controller implementation.

Remove registerClass code from the default view controller code snippet

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top