Note that there are some explanatory texts on larger screens.

plurals
  1. POUICollectionView Not Appearing
    primarykey
    data
    text
    <p>I am trying to set up <code>UICollectionView</code> <strong>programatically</strong> in my view controller which extends <code>UIViewController</code>. For some reason, my collection view is not showing up at all. Below is what I have.</p> <p>Why is it not appearing? I am hooking it up to the delegate and data source and adding it as a subview to <code>self.view</code>. What's missing in my code?</p> <p><strong>In my <code>.h</code> file:</strong></p> <pre><code>@interface MainViewController : UIViewController { @private UICollectionView *_collectionView; NSMutableArray *_results; // data source array } @end </code></pre> <p><strong>In my <code>.m</code> file:</strong></p> <pre><code>@interface MainViewController () &lt;UICollectionViewDataSource, UICollectionViewDelegateFlowLayout&gt; @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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload