Question

I am trying to set up UICollectionView programatically in my view controller which extends UIViewController. For some reason, my collection view is not showing up at all. Below is what I have.

Why is it not appearing? I am hooking it up to the delegate and data source and adding it as a subview to self.view. What's missing in my code?

In my .h file:

@interface MainViewController : UIViewController
{
    @private
    UICollectionView *_collectionView;
    NSMutableArray *_results; // data source array
}
@end

In my .m file:

@interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *results;
@end

@implementation MainViewController

@synthesize collectionView = _collectionView;
@synthesize results = _results;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // some init stuff - nothing to do with collection view.
    }

    return self;
}

- (void)loadView
{
    self.results = [NSMutableArray array];
    UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
    [self.results addObject:image1];
    [self.results addObject:image2];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView = collectionView;

    [self.view addSubview:self.collectionView];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView reloadData];

}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return [self.results count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = [self.results objectAtIndex:indexPath.row];    
    return CGSizeMake(image.size.width, image.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20);
}
Was it helpful?

Solution

I got errors trying to run your code unless I changed the loadView method to viewDidLoad -- according to the docs you're not supposed to directly call loadView. To get the data source and delegate methods to run, I moved the lines setting the delegate and data source to self below where you set self.collectionView = collectionView

    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

OTHER TIPS

Your numberOfSectionsInCollectionView: returns 0. For a collection view with one section, you should either return 1 or just not implement this method.

Also I cannot see where you alloc/init self.collectionView.

I ended up subclassing UICollectionViewController instead of UIViewController and changing the init method to:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

and it worked.

You only had to bind your CollectionView delegate, and dataSource to ViewController in the StoryBoard.enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top