0

UIViewController含む がありますCollectionViewが、出力がすべて白になると

GridViewController.h 内

 #import <UIKit/UIKit.h>

 @interface GridViewController : UIViewController <UICollectionViewDataSource,
   UICollectionViewDelegate>{

  }
 @property (nonatomic, strong)UIImageView *imageHeader;
 @property (nonatomic, strong)UIButton * buttonHome;
 @property (nonatomic, strong)UILabel * labelTitle;
 @property (nonatomic, strong)UICollectionView * collectionView;

 //....
 @end

GridViewController.m 内

 - (void)viewDidLoad
 {
    //....
     [self.collectionView registerClass:[UICollectionView class] 
         forCellWithReuseIdentifier:@"Cell"];

      NSLog(@"%@", self.collectionView);//here (null), why?
     self.collectionView.delegate=self;
     self.collectionView.dataSource=self;
    //...
  }

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

 - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection: 
 (NSInteger)section;
{
     return 32;
 }



- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath{

     NSString *kCellID = @"cellID";
     CollectionViewCellCustom *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    cell.imageView.backgroundColor =[UIColor greenColor];


    return cell;
 }
4

2 に答える 2

4

あなたのコードにはアウトレットがありませんでした。したがって、プログラムで作成しようとしていると仮定しています。そのためにあなたがすべきこと

UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubView:self.collectionView];  
 [self.collectionView registerClass:[UICollectionViewCell class]
        forCellWithReuseIdentifier:@"Cell"];
 self.collectionView.delegate=self;
 self.collectionView.dataSource=self;

registerClass:[UICollectionView class]あなたのコードでは、どちらが間違っているかが正しいことがわかりますregisterClass:[UICollectionViewCell class]

変化する

    [self.collectionView registerClass:[UICollectionView class]forCellWithReuseIdentifier:@"Cell"];

  [self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];

別のセル ID を使用しているもう 1 つの間違いは、登録とデキューです。同じようにします。ID Cellでセルを登録し、 cellIDを使用して deque しようとしています

于 2013-03-13T11:24:01.533 に答える