Home Adding UICollectionViewController to UIViewController Not Working
Reply: 0

Adding UICollectionViewController to UIViewController Not Working

user8780
1#
user8780 Published in September 19, 2018, 6:44 pm

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.

share|improve this question
  • Looks like your implementation is correct. Try giving some background colour to your cell in cellForItemAtIndexPath method. – anil Nov 24 '15 at 11:28
  • set the frame of the child view controller – Andrei Durnea Feb 27 at 10:31

2 Answers 2

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.340052 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO