1

나는 컬렉션 뷰의 이미지를 Facebook에서와 마찬가지로 각 이미지에 레이블을 추가하여 표시해야하는 iOS 애플리케이션으로 작업하고 있습니다. 컬렉션의 이미지를 클릭하면 Facebook에서는 이미지를 확장하고 레이블이있는 이미지를 표시합니다. 다른 이미지 크기는 크기가 다르게 나타납니다. 나도 마찬가지야. 모든 것이 잘 작동하지만, 작은 이미지를 추가하면 셀의 레이블 위치가 바뀝니다. 큰 이미지에는 두 개의 레이블이 표시되고, 한 개의 레이블은 원하는 위치에, 두 번째 작은 이미지 레이블에는 두 번째 레이블이 나타납니다. 이 코드로 작업하고 있습니다 :다른 위치에있는 UICollectionView 셀에 라벨 추가하기 iOS

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    int i; 
    Cell *cell; 
    UILabel * lblName ; 
    UILabel * lblName1 ; 
    cell=[collectionView dequeueReusableCellWithReuseIdentifier:REUSEABLE_CELL_IDENTITY forIndexPath:indexPath]; 
    @try 
    { 
    NSLog(@"index path is %ld",(long)indexPath.row); 

     if(rearr.count == 0) 
      { 
      cell.imageviews.image=NULL; 
      } 

     else 
      { 
      NSLog(@"count %lu",(unsigned long)rearr.count); 

     for (i=0;i<rearr.count; i++) 
      { 
        NSLog(@"count %lu",(unsigned long)rearr.count); 
       image = [UIImage imageWithContentsOfFile:rearr[i]]; 
        NSLog(@"image.size.width1 : %f",image.size.width); 
        NSLog(@"image.size.height1 : %f",image.size.height); 

       if(image.size.width > image.size.height) 
       { 

       lblName = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 230.0, 300.0,80)]; 
       lblName.text = @"Image"; 
       lblName.textAlignment = NSTextAlignmentCenter; 
       [lblName setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:10]]; 
       [lblName setTextColor:[UIColor colorWithRed:137.0f/255.0 green:137.0f/255.0 blue:137.0f/255.0 alpha:1.0]]; 
       [lblName setBackgroundColor:[UIColor whiteColor]]; 
       [cell.contentView addSubview:lblName]; 
       } 
       else 
       { 

       lblName1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)]; 
       lblName1.text = @"Image1"; 
       lblName1.textAlignment = NSTextAlignmentCenter; 
       [lblName1 setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:10]]; 
       [lblName1 setTextColor:[UIColor colorWithRed:137.0f/255.0 green:137.0f/255.0 blue:137.0f/255.0 alpha:1.0]]; 
       [lblName1 setBackgroundColor:[UIColor whiteColor]]; 
       [cell.contentView addSubview:lblName1]; 

       } 

      if(image != nil) 
       { 
       [itms addObject:image]; 
       } 
      else 
      { 
      NSData *data = [[NSData alloc]initWithContentsOfFile:rearr[i]]; 
      image=[UIImage imageWithData:data]; 
      [itms addObject:image]; 
      } 
     } 

     cell.imageviews.image=[itms objectAtIndex:indexPath.row]; 
     cell.imageviews.layer.cornerRadius=5.0f; 
     cell.imageviews.clipsToBounds = YES; 
    } 


    return cell; 
    } 
    @catch (NSException *exception) 
    { 

     NSLog(@"image insertion in collecvcontroller : exception %@",exception); 
     return cell; 

    } 


} 

도와주세요. 내가 뭘 잘못하고 있는지 모르겠다.

+0

질문이있는 스크린 샷을 제공하면 더 좋은 아이디어를 얻을 수 있습니다. – Mrunal

답변

0

dequeueReusableCellWithReuseIdentifier을 사용하는 동안 매번 UILabel을 추가하는 이유는 무엇입니까? 아래에서 원하는 결과를 볼 수 있습니다.

if(image.size.width > image.size.height) 
{ 
    [[[cell contentView] viewWithTag:2] removeFromSuperView]; 
    lblName = [[cell contentView] viewWithTag:1]; 
    if(! lblName) 
    { 
     lblName = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)]; 
     [lblName setTag:1]; 
     [[cell contentView] addSubview:lblName]; 
    } 
    // Do other stuff here 
} 
else 
{ 
    [[[cell contentView] viewWithTag:1] removeFromSuperView]; 
    lblName1 = [[cell contentView] viewWithTag:2]; 
    if(! lblName1) 
    { 
     lblName1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 410.0, 300.0,80)]; 
     [lblName1 setTag:2]; 
     [[cell contentView] addSubview:lblName1]; 
    } 
    // Do other stuff here 
} 
+0

@ pal : 답장을 보내 주셔서 감사합니다.하지만 다른 케이스에 다시 들어가는 것은 아닙니다. [UILabel alloc] initWithFrame : CGRectMake (0.0, 410.0, 300.0,80)]; –

+0

당신이하고 싶은 것이 확실하지 않습니다. 나는 게시 한 코드를 작성하는 올바른 방법과 최적화 된 방법을 보여줄뿐입니다. else 부분이 필요하지 않으면 제거하십시오. –