2017-02-08 16 views
0

私はコレクションビューに新しいセルを追加するこのコードを持っています。デリゲートとデータソースが正しく設定されています。しかしコレクションビューのセルには何も表示されませんでした。私がデバッグすると、セルが作成されたことが示されますが、セルにはUIView以外が含まれています。UIButtonにはUIImageViewが含まれているはずです。 registerClassObjective Cで列挙型の数値を取得する方法、または列挙型を数値で設定する方法は?

  • this link

    - (void)viewDidLoad { 
        [super viewDidLoad]; 
        [self setImgGallery:[[NSMutableArray alloc] init]]; 
        [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"new"]; 
        [[self cvPictureGallery] registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"review"]; 
    
        // add 5 UIImage test to imgGallery. 
        for (int i = 0; i < 5; i++) { 
         [[self imgGallery] addObject:[UIImage named:@"test.png"]]; 
        } 
    } 
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
        return MIN ([[self imgGallery] count] + 1, 5); 
    } 
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
        if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon 
         UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath]; 
    
         if (cell == nil) { 
          /* 
          cell 
          - contentView 
           - button 
           - camera icon 
          */ 
    
          cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    
          UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]]; 
          [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)]; 
          [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit]; 
    
          UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
          [btnCamera addSubview:btnCamera]; 
          [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside]; 
          [imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 
    
          [cell addSubview:btnCamera]; 
          [cell setBackgroundColor:[UIColor whiteColor]]; 
          [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"]; 
         } 
         return cell; 
        } 
        else { 
         UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath]; 
         if (cell == nil) { 
          /* 
          cell 
          - contentView 
           - button 
           - image selected 
          */ 
    
          cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    
          UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; 
          [imgSelected setFrame:CGRectMake(0, 0, 100, 100)]; 
          [imgSelected setContentMode:UIViewContentModeScaleAspectFill]; 
          [imgSelected setTag:1]; 
    
          UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
          [btnCamera addSubview:btnCamera]; 
          [btnCamera addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside]; 
          [imgSelected setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 
    
          [cell addSubview:btnCamera]; 
          [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"]; 
         } 
         return cell; 
        } 
    } 
    

    は、私はこれらのQA上の参照を使用していました。

EDIT:そこに受け入れ答えとの議論に基づいて、これはまだ何も表示されません私の更新されたコード、次のとおりです。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon 
     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath]; 

     if (cell == nil) { 
      /* 
      cell 
      - contentView 
       - button 
       - camera icon 
      */ 

      cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 

      UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]]; 
      [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)]; 
      [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit]; 
      [imgCameraIcon setTag:1]; 

      UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
      [btnCamera addSubview:imgCameraIcon]; 
      [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside]; 
      [btnCamera setTag:2]; 
      [imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 

      [cell addSubview:btnCamera]; 
      [cell setBackgroundColor:[UIColor whiteColor]]; 
      [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"]; 
     } 

     UIImageView * imgCameraIcon = (UIImageView *) [cell viewWithTag:1]; 
     [imgCameraIcon setImage:[UIImage imageNamed:@"camera.png"]]; 

     NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]); 

     return cell; 
    } 
    else { 
     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath]; 
     if (cell == nil) { 
      /* 
      cell 
      - contentView 
       - button 
       - image selected 
      */ 

      cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 

      UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; 
      [imgSelected setFrame:CGRectMake(0, 0, 100, 100)]; 
      [imgSelected setContentMode:UIViewContentModeScaleAspectFill]; 
      [imgSelected setClipsToBounds:YES]; 
      [imgSelected setTag:1]; 

      UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
      [btnImage addSubview:imgSelected]; 
      [btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside]; 
      [btnImage setTag:2]; 
      [imgSelected setCenter:CGPointMake([btnImage width]/2, [btnImage height]/2)]; 

      [cell addSubview:btnImage]; 
     } 

     UIImageView * imgSelected = (UIImageView *) [cell viewWithTag:1]; 
     [imgSelected setImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; 

     NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]); 

     return cell; 
    } 
} 

サブビュー・カウントの結果は常に以下1で、あります作業中のものですが、サブビュー数は毎回増えています。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon 
     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath]; 

     if (cell == nil) { 
      /* 
      cell 
      - contentView 
       - button 
       - camera icon 
      */ 

      cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     } 

     UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]]; 
     [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)]; 
     [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit]; 

     UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     [btnCamera addSubview:imgCameraIcon]; 
     [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside]; 
     [imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 

     [cell addSubview:btnCamera]; 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
     [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"]; 

     NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]); 

     return cell; 
    } 
    else { 
     UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath]; 
     if (cell == nil) { 
      /* 
      cell 
      - contentView 
       - button 
       - image selected 
      */ 

      cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     } 

     UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; 
     [imgSelected setFrame:CGRectMake(0, 0, 100, 100)]; 
     [imgSelected setContentMode:UIViewContentModeScaleAspectFill]; 
     [imgSelected setClipsToBounds:YES]; 
     [imgSelected setTag:1]; 

     UIButton * btnImage = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     [btnImage addSubview:imgSelected]; 
     [btnImage addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside]; 
     [imgSelected setCenter:CGPointMake([btnImage width]/2, [btnImage height]/2)]; 

     [cell addSubview:btnImage]; 
     [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"]; 

     NSLog (@"Subview count:%lu", (unsigned long)[[cell subviews] count]); 

     return cell; 
    } 
} 
+0

collectionview登録クラスが上にする必要があります見てください? – Joshua

+1

@Joshua選択した正解の解決策を実行した後、正しく結果が得られます。私はあなたがそのcollectionViewの生涯に一度それを登録する必要があると思います。 –

+0

しかし、あなたの選択した答えを読んで、私はそれが正しくセルを再利用しないと思う。 – Joshua

答えて

1

[btnCamera addSubView:btnCamera] 

を私はあなたが間違っを書くと思いますセルを作成するコード。以下のように同じようにコードを更新し、あなたが実際にdequeReusableCellを呼び出す前に出力

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
if ([indexPath row] == [[self imgGallery] count]) { // show the camera icon 
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"new" forIndexPath:indexPath]; 

    if (cell == nil) { 
     /* 
     cell 
     - contentView 
      - button 
      - camera icon 
     */ 

     cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    } 
     UIImageView * imgCameraIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera.png"]]; 
     [imgCameraIcon setFrame:CGRectMake(0, 0, 50, 50)]; 
     [imgCameraIcon setContentMode:UIViewContentModeScaleAspectFit]; 

     UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     [btnCamera addSubview:btnCamera]; 
     [btnCamera addTarget:self action:@selector(openCameraTapped) forControlEvents:UIControlEventTouchUpInside]; 
     [imgCameraIcon setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 

     [cell addSubview:btnCamera]; 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
     [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"new"]; 

    return cell; 
} 
else { 
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"review" forIndexPath:indexPath]; 
    if (cell == nil) { 
     /* 
     cell 
     - contentView 
      - button 
      - image selected 
     */ 

     cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
} 
     UIImageView * imgSelected = [[UIImageView alloc] initWithImage:[[self imgGallery] objectAtIndex:[indexPath row]]]; 
     [imgSelected setFrame:CGRectMake(0, 0, 100, 100)]; 
     [imgSelected setContentMode:UIViewContentModeScaleAspectFill]; 
     [imgSelected setTag:1]; 

     UIButton * btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     [btnCamera addSubview:btnCamera]; 
     [btnCamera addTarget:self action:@selector(imageTapped) forControlEvents:UIControlEventTouchUpInside]; 
     [imgSelected setCenter:CGPointMake([btnCamera width]/2, [btnCamera height]/2)]; 

     [cell addSubview:btnCamera]; 
     [collectionView registerClass:[cell class] forCellWithReuseIdentifier:@"review"]; 
    return cell; 
} 

}

+0

あなたは正しいです。私はブラケットの中にすべてを入れるのを忘れた。どうもありがとうございました! –

+0

ちょっと不思議そうですね、セルをもう一度表示すると、もう一度ボタンが追加されますか?これはセルを適切に再利用しません。それはちょうどあなたがあまりにも多くをスクロールし、それが遅くなるときに気づくでしょう、それはbtnCameraを何度も何度も追加します。 – Joshua

+0

@ジョシュアよ、あなたは本当に正しい。 'NSLog(@ Subview count:%lu"、(unsigned long)[[cell subviews] count]);をセルを返す直前に追加し、 'reloadData'を呼び出すたびにサブビューカウントが増加することを示します。しかし、すべてを角括弧の中に入れ、角括弧の外側に 'viewWithTag:'を使って呼び出すと、そこに画像を読み込むと、まだ何も見えません。サブビュー数は1のままです。変更したコードを表示できるようにコードを更新しました。 –

2

私はあなたのクエリに関連して、とにかくであなたの質問のタイトルを確認していないが、ライン交換してください:

[btnCamera addSubView:imgCameraIcon] 
+0

これは非常に役に立ちます。それは私の一部でスリップです。ありがとう! –

関連する問題