1
votes

I have a UIViewController and a UICollectionViewController. I add the UICollectionViewController to the UIViewController using the following:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

self.toolsCollectionViewController = [[ToolsCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
self.toolsCollectionViewController.view.backgroundColor = [UIColor clearColor];

[self addChildViewController:self.toolsCollectionViewController];
[self.view addSubview:self.toolsCollectionViewController.view];
[self.toolsCollectionViewController didMoveToParentViewController:self];

The problem is that when I add the collection I get a solid black screen appear with no cells on display

#import "ToolsCollectionViewController.h"

@interface ToolsCollectionViewController ()

@end

@implementation ToolsCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[ToolsCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSLog(@"Cell");
    // Configure the cell

    return cell;
}

#pragma mark 

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@end

I have created an NSLog for the loop through the cells and that seems to be working fine. Not sure what I am missing.

2
Looks like your implementation is correct. Try giving some background colour to your cell in cellForItemAtIndexPath method.anil
set the frame of the child view controllerDurdu

2 Answers

0
votes

Are you correctly setting up the collectionView's delegate and dataSource? Without those two a collectionView will appear empty and will only display a rectangle colored with the background color.

Most likely what you need is to place the following in the - (void)viewDidLoad above: self.collectionView.delegate = self; self.collectionView.dataSource = self;

0
votes

There could be 3 different scenarios.

  1. Color schemes you have implemented is causing the issue
  2. Delegate and Datasource have not been called properly.
  3. either your cell not exist or reuse identifier is not valid.