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

我也是不知道你想做什麼。我只是顯示你寫你的代碼已經發布的權利和最優化的方式。如果其他部分不需要然後刪除它。 –