2017-02-08 2 views
0

이 코드는 컬렉션보기에서 새 셀을 추가하는 데 사용됩니다. 대리자 및 데이터 소스가 올바르게 설정되었습니다. 그러나 콜렉션 뷰 셀에는 아무 것도 표시되지 않았습니다. 글쎄, 내가 그것을 디버깅 할 때 세포가 생성되었음을 보여 주지만 세포는 UIView 이외의 것을 포함하고 있는데, 그 안에 UIButtonUIImageView이 포함되어 있어야한다고 기대한다. 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; 
        } 
    } 
    

    나는 이러한 품질에 대한 참조를 사용했다.

편집 : 허용 대답 거기 토론을 기반으로, 이것은 아직 아무것도 표시되지 않습니다 내 업데이트 된 코드입니다 :

- (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

@ Joshua yea 네 말이 맞아. 셀을 반환하기 직전에'NSLog (@ Subview count : % lu ", (unsigned long) [[셀 서브 뷰] 개수]);를 추가하면'reloadData'를 호출 할 때마다 서브 뷰 수가 증가하는 것으로 나타납니다. 그러나 모든 것을 꺽쇠 괄호 안에 넣고 괄호 밖에서'viewWithTag :'를 사용하여 각 괄호 안에 넣은 다음 이미지를로드하면 여전히 아무 것도 볼 수 없습니다. 하위 뷰 수는 1로 유지됩니다. 수정 된 코드를 볼 수 있도록 코드를 업데이트했습니다. –

2

내가 쿼리에 관련되어 어쨌든 귀하의 질문의 제목 모르겠지만 라인 교체하십시오 :

[btnCamera addSubView:imgCameraIcon] 
+0

이것은 매우 유용합니다. 그건 내 실수 야. 감사! –