J'essaie de mettre en place UICollectionView
par programme dans mon contrôleur de vue qui s'étend UIViewController
. Pour une raison quelconque, ma vue de collection n'apparaît pas du tout. Voici ce que j'ai.UICollectionView n'apparaît pas
Pourquoi n'apparaît-il pas? Je l'accroche au délégué et à la source de données et l'ajoute en tant que sous-vue à self.view
. Qu'est-ce qui manque dans mon code?
Dans mon fichier .h
:
@interface MainViewController : UIViewController
{
@private
UICollectionView *_collectionView;
NSMutableArray *_results; // data source array
}
@end
Dans mon fichier .m
:
@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);
}
Cela n'a pas fonctionné. J'ai fini par sous-classer 'UICollectionViewController' et en changeant la méthode' init' à celle qui est conforme à 'UICollectionViewController' et cela a fonctionné. J'ai des problèmes avec 'cellForIndexPath' - je vais poster un nouveau post pour ça. – darksky
Je ne sais pas pourquoi cela n'aurait pas fonctionné pour vous - je viens de faire une nouvelle application, mettre dans votre code, et fait les 2 changements que j'ai mentionnés, et ça a bien fonctionné. – rdelmar
C'est bizarre. Êtes-vous capable d'afficher des objets aussi? J'ai des problèmes avec mon 'cellForItemInIndexPath'. Il me donne une erreur 'could not dequeue'. N'utilisez-vous pas aussi xib/storyboard? – darksky