Chenna Chenna - 2 months ago 27
iOS Question

collectionView didSelectItemAtIndexPath with sending images

i am showing images in collection view. when i am selecting particular cell the detailed image will show in another viewcontroller. the delegate method is didSelectItemAtIndexPath. please help me.

recipeImages = [NSArray arrayWithObjects:@"img2.png", @"img.png", @"img1.png", @"img3.png", @"img4.png", @"img5.png", @"img6.png", @"img7.png", nil];

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView1 cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"Cell";

CustomCell *cell = (CustomCell *)[collectionView1 dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

[cell.imageView setImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];

return cell;
}

Answer

Check the below code:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {

      NSString *senderImg=[recipeImages objectAtIndex:indexPath.item];
      NextViewController *next  = [[NextViewController alloc]   initWithNibName:@"NextViewController" bundle:nil]; 
    next.receivedImage=senderImg;
     [self presentModalViewController:next animated:NO];
      }

NextViewController.h

@property(nonatomic,retain)NSString *receivedImage;

NextViewController.m

@syntosize receivedImage;

-(void)viewDidLoad
{
   UIImage imageDispay=[UIImage imageNamed:receivedImage];
}
Comments