UICollectionViewが表示されない
-
11-12-2019 - |
質問
セットアップしようとしています UICollectionView
プログラム的に 拡張するビューコントローラーで UIViewController
. 。何らかの理由で、コレクション ビューがまったく表示されません。以下は私が持っているものです。
なぜ表示されないのでしょうか?これをデリゲートとデータソースに接続し、サブビューとして追加しています。 self.view
. 。私のコードには何が足りないのでしょうか?
私の中で .h
ファイル:
@interface MainViewController : UIViewController
{
@private
UICollectionView *_collectionView;
NSMutableArray *_results; // data source array
}
@end
私の中で .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);
}
解決
ロードビューメソッドをViewDidLoadに変更しない限り、コードを実行しようとしているエラーが発生しました - Docsに従ってロードビューを直接呼び出すことになっていません。データソースとデリゲートメソッドを実行するには、DELEGATEとデータソースをSelf.CollectionView= CollectionView を設定した状態に移動しました。
self.collectionView = collectionView;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
. 他のヒント
numberOfSectionsInCollectionView:
は0
を返します。1つのセクションを使用したコレクションビューの場合は、1
を返すか、このメソッドを実装しないでください。
alloc / init self.collectionView
を見ることができません。
結局、代わりに UICollectionViewController をサブクラス化しました。 UIViewController
init メソッドを次のように変更します。
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
そしてそれはうまくいきました。
ストーリーボードのDataSourceをBideSourceにバインドしなければなりませんでした。画像の入力ここで