ios - 在不同位置的 UICollectionView 单元格上添加标签 iOS

标签 ios iphone uicollectionview uicollectionviewcell uicollectionviewlayout

我正在使用一个 iOS 应用程序,在该应用程序中,我必须通过在每个图像上添加标签来在 Collection View 中显示图像,与在 Facebook 中一样,当我们在 Facebook 中单击集合中的图像时,它会展开图像并显示带有标签的图像。不同的图像尺寸显示的尺寸不同。我也在做同样的事情。一切正常,但是当我添加小图像时,它会改变单元格上标签的位置。这意味着大图像上出现两个标签,一个标签正好在我想要的位置,第二个小图像标签。我正在使用这段代码:

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

    }


}

请帮帮我。我不知道我做错了什么。

最佳答案

虽然您使用了 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
}

关于ios - 在不同位置的 UICollectionView 单元格上添加标签 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776039/

相关文章:

ios - 滚动直到 uicollectionview 的最后一个单元格

IOS UICollectionView 和 UIScrollView 回收问题

swift - 如何从 Collection View 中删除项目?

ios - 根据 UIScrollView 内的视口(viewport)裁剪 UIImage

ios - Xamarin iOS GPS 错误

ios - SpriteBuilder 时间线自动播放动画未运行

ios - 有没有办法为 UICollectionView 中的特定部分应用自定义流布局?

iphone - Objective-C 稀疏数组 redux

ios - 使用 ARC,是否需要释放 CGMutablePath Refs?

iphone - 如何在应用程序启动之间存储整数数组?