Tulon
Tulon

Reputation: 4124

UICollectionViewCell sending data to another viewController

I have a UICollectionView. Here I am showing some photos from my MainBundle. The location or directory of these photos are saved in a SQLite Database. I am using segue. The problem is when I try to send the image link to detailsViewController it is not sending the link.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    selectedPhotoIndex = indexPath.row;
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
        [photoDetailsViewController setImagePath:pProperty.link];
        //selectedPhotoIndex = 0;
    }
}

But if I put the code with out segue.identifier it send the link to the detailsViewController.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
        [photoDetailsViewController setImagePath:pProperty.link];
        //selectedPhotoIndex = 0;
}

But here the problem is, it hold the reference of previous link some where and to show the correct image I have to tap again on the selected thumb Line image. I try with these alternatives but no luck.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
        CustomCell *cell = (CustomCell *) sender;
        NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];

        //NSLog(@"indexPath %@",indexPath);
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];
        [photoDetailsViewController setImagePath:pProperty.link];
    }
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
    {
        CustomCell *cell = (CustomCell *) sender;
        NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];

        int num = indexPath.row %10;
        NSLog(@"num %i", num);

        PhotoDetailsViewController *photoDetailsViewController = (PhotoDetailsViewController *)[segue destinationViewController];
        PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:num];
        [photoDetailsViewController setImagePath:pProperty.link];
    }
}

Here is my full code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.sQLite = [[SQLite alloc] init];
    [self.sQLite callDataBaseAndPhotoTableMethods];

    self.arrayOfPhotos = [[NSMutableArray alloc] init];
    self.arrayOfPhotos = [self.sQLite returnDataFromPhotoTable];

    self.photoCollectionView.delegate = self;
    self.photoCollectionView.dataSource = self;
}

/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.arrayOfPhotos count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    self.photoProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];

    // NSLog(@"id %d", photoProperty.idNumPHOTO);
    // NSLog(@"name %@", photoProperty.name);
    // NSLog(@"links %@", photoProperty.link);

    NSData *imageData = [NSData dataWithContentsOfFile:photoProperty.link];
    UIImage *originalImage = [[UIImage alloc] initWithData:imageData];

    // This is the size where I get from the Cell UIImageView hight & wodth
    CGSize size = CGSizeMake(36.0f, 51.0f);
    UIImage *thumbNailImage = [self imageWithImage:originalImage scaledToSize:size];

    cell.imageView.image = thumbNailImage;

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    return cell;
}

If any one understand my problem and have any solution please share it with me. Thanks a lot in advanced.
Have a nice day.

Upvotes: 1

Views: 133

Answers (2)

Henit Nathwani
Henit Nathwani

Reputation: 442

Check for imagePath in viewDidAppear method of your destination view controller.

First viewDidLoad of destination view controller will be called, then prepareForSegue of previous controller will be called. So if you're utilizing the imagePath in viewDidLoad of destination view controller then you won't get it's value.

Hope this helps..

Upvotes: 0

sahara108
sahara108

Reputation: 2859

The problem is that you are using wrong property of segue. The identifier is used for performSegueWithIdentifier:. You use destinationViewController to get your destination and check it like this [segue.destinationViewController isKindOfClass:NSClassFromString(@"PhotoDetailsViewController")]

Upvotes: 1

Related Questions