rootViewController を持つ iOS アプリがあります。このView Controllerには、さまざまなビューがあります。今、このビューにコレクション ビューが必要です。どうすればいいですか?私が試したことはです。ストーリーボードに uicollection ビューを追加し、データソースとデリゲートを新しい uicollectionview コントローラーに設定しました。基本的なものは動作します。しかし、uicollectionview コントローラー ビューから collectionview にアクセスできません。self.collectionview. 私は何かを忘れましたか?または、これを処理するより良い方法はありますか?
3 に答える
コレクションView Controllerではなく、プレーンView Controllerを使用する必要があります。後者は最上位のview
プロパティとしてコレクション ビューを持つため、他のビューを追加する柔軟性が低くなります。
必要self.collectionView
に応じて、プロパティを宣言する必要があります。インターフェイス ビルダー (およびデリゲートとデータソース) に接続することを忘れないでください。
@property (nonatomic, strong) IBOutlet UICollectionView * collectionView;
もちろん、複数のコレクション ビューで同じことができます。ただし、UICollectionViewDataSource
メソッドとデリゲート メソッドでは、2 つのコレクション ビューを区別し、それぞれのデータを提供するか、どちらのコレクション ビューが使用されたかに従ってユーザー インタラクションに対応する必要があります。
コレクション ビューをルート ビュー コントローラー (ビュー階層の一部) で管理する場合は、コレクション ビュー コントローラーを忘れて、コレクションのデータ ソースとデリゲートをルート ビュー コントローラーに設定します。次に、コレクション ビュー データ ソースとデリゲート プロトコルをルート ビュー コントローラー クラスに実装します。
@interface ViewController ()
@property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
@end
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(200, 200)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView setCollectionViewLayout:flowLayout];
cellForItemAtIndexPath で..以下のようなものでなければなりません..
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
[titleLabel setText:cellData];
return cell;
}
次のアーティクルを参照してください。うまく説明されています。 http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/