V
V
V2015-01-11 20:57:10
Objective-C
V, 2015-01-11 20:57:10

Why is UICollectionView not showing?

I did everything according to the tutorial ( www.appcoda.com/ios-programming-uicollectionview-t... i.e. in the storyboard I made a new UICollectionViewController with one cell (I didn’t forget about Reuse Identifier: @"Cell"). I placed a UIImage with the tag in it 100. Then created a class:

#import <UIKit/UIKit.h>

@interface RecipeCollectionViewController : UICollectionViewController <UICollectionViewDelegate, UICollectionViewDataSource>

@end

#import "RecipeCollectionViewController.h"

@interface RecipeCollectionViewController (){
    
          NSArray *recipePhotos;
}


@end

@implementation RecipeCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    recipePhotos = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", nil];

    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell 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 <UICollectionViewDataSource>

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


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return recipePhotos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
    
    return cell;
}

but in the end I get only a black screen. Xcode 6, if anything.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question