简体   繁体   中英

Adding Label on UICollectionView Cell at different positions iOS

I am working with an iOS application in which i have to show images in collection-view by adding a label on each image, same as in Facebook, when we click on images in collection in Facebook it expands its images and show the images with the label. Different images size appears differently in size. I a doing same . Everything is working fine , but when i added small images it changes the position of label on cells .It means two label appears on the big images , one label exactly where i want and second small image label. I am working with this code :

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

    }


}

Please help me out . I don't know what am i doing wrong.

While you used dequeueReusableCellWithReuseIdentifier then why you are adding the UILabel every time. Do the below and you'll see your desire output..

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM