-1

rootViewController を持つ iOS アプリがあります。このView Controllerには、さまざまなビューがあります。今、このビューにコレクション ビューが必要です。どうすればいいですか?私が試したことはです。ストーリーボードに uicollection ビューを追加し、データソースとデリゲートを新しい uicollectionview コントローラーに設定しました。基本的なものは動作します。しかし、uicollectionview コントローラー ビューから collectionview にアクセスできません。self.collectionview. 私は何かを忘れましたか?または、これを処理するより良い方法はありますか?

4

3 に答える 3

2

コレクションView Controllerではなく、プレーンView Controllerを使用する必要があります。後者は最上位のviewプロパティとしてコレクション ビューを持つため、他のビューを追加する柔軟性が低くなります。

必要self.collectionViewに応じて、プロパティを宣言する必要があります。インターフェイス ビルダー (およびデリゲートとデータソース) に接続することを忘れないでください。

@property (nonatomic, strong) IBOutlet UICollectionView * collectionView; 

もちろん、複数のコレクション ビューで同じことができます。ただし、UICollectionViewDataSourceメソッドとデリゲート メソッドでは、2 つのコレクション ビューを区別し、それぞれのデータを提供するか、どちらのコレクション ビューが使用されたかに従ってユーザー インタラクションに対応する必要があります。

于 2013-07-27T08:24:24.940 に答える
0

コレクション ビューをルート ビュー コントローラー (ビュー階層の一部) で管理する場合は、コレクション ビュー コントローラーを忘れて、コレクションのデータ ソースとデリゲートをルート ビュー コントローラーに設定します。次に、コレクション ビュー データ ソースとデリゲート プロトコルをルート ビュー コントローラー クラスに実装します。

于 2013-07-27T02:30:20.447 に答える
0
@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/

于 2013-08-31T21:11:34.760 に答える