0

I have added a UICollectionView to a UIViewController, but it does not show up, why? I can see only the black background. Labels textcolor is white.

enter image description here

UPDATE

I had to implement the following UICollectionViewDataSource methods. The lesson that UICollectionView can not design via static cells, like UITableView, only via Dynamic Prototypes.

func collectionView(collectionView: UICollectionView!,
    numberOfItemsInSection section: Int) -> Int
{
    return 5
}

func collectionView(collectionView: UICollectionView!,
    cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)

    return cell as UICollectionViewCell
}
János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

1

Looks like you not implemented CollectionView delegate methods. I would like to suggest, you have to implement UICollectionView delegate methods.

Those are -

#pragma mark - UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return "number of items";
}

// This method return the object of collectionViewCell.Your collectionviewcell has label object.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    return cell;
}

// Perform operation on selection of cell
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1; // returns number of section
}

#pragma mark - UICollectionViewFlowDelegate

// Returns size of Cell
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(80, 60);
}

// Space between cells
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(2, 5, 2, 5);
}

Don't forget to link delegate to xib file. After implementing those methods you can able to see those Cell's on device or simulator.

Vikas Sawant
  • 106
  • 1
  • 8