Note that there are some explanatory texts on larger screens.

plurals
  1. POget Image from cell in a UICollectionView on tap
    text
    copied!<p>So what I want to achieve is when a user taps on a cell from the UICollectionView, the image from this cell is displayed on the next UIView.</p> <p>To implement this I used the delegate method <code>-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath</code> and NSUserDefaults. This is how I am doing it</p> <ol> <li>Get the cell tapped</li> <li>Get the image from the UIImageView of the cell from #1</li> <li>Convert the image into NSData</li> <li>Put the data into NSUserDefaults</li> <li>Perform the segue to the next view controller</li> <li>Get the data from the NSUserDefaults</li> <li>Convert to UIImage and display in a UIImageView.</li> </ol> <p>Here is the code:</p> <pre><code>-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; NSData *data = [[NSData alloc]initWithData:UIImagePNGRepresentation(cell.ItemImageView.image)]; NSLog(@"Before Data %@", data); NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; [def setObject:data forKey:@"feedData"]; [def synchronize]; [self performSegueWithIdentifier:@"itemTappedSegue" sender:self]; } - (void)viewDidLoad { [super viewDidLoad]; NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSData *data = [def objectForKey:@"feedData"]; NSLog(@"After Data:%@", data); UIImage *image = [[UIImage alloc]initWithData:data]; self.imageView.image = image; } </code></pre> <p>Should work but is not. I get random results. Sometimes there is no image in the next UIView, sometimes there is an image but its not the one I tapped on. </p> <p>EDIT::here is the implementation for <code>cellForItemAtIndexpath</code></p> <pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; if(response2.count &gt; 0){ cell.usernameLabel.text = [self getUsername:[response2 objectAtIndex:indexPath.item]]; [UIApplication sharedApplication].networkActivityIndicatorVisible =YES; dispatch_queue_t getUserAvatar = dispatch_queue_create("Avatar downloader", NULL); dispatch_queue_t getFeed = dispatch_queue_create("Feed downloader", NULL); dispatch_async(getUserAvatar, ^{ NSString *urlString = [self getAvatarUrl:[response2 objectAtIndex:indexPath.item]]; NSURL *url = [[NSURL alloc]initWithString:urlString]; NSData *avatarData = [NSData dataWithContentsOfURL:url]; dispatch_async(dispatch_get_main_queue(), ^{ cell.DPImageView.layer.masksToBounds = YES; cell.DPImageView.layer.cornerRadius = 6.0f; cell.DPImageView.image = [UIImage imageWithData:avatarData]; }); }); dispatch_async(getFeed, ^{ NSString *URLString = [self getFeedUrl:[response2 objectAtIndex:indexPath.item]]; NSURL *URL = [[NSURL alloc]initWithString:URLString]; NSData *feedData = [NSData dataWithContentsOfURL:URL]; dispatch_async(dispatch_get_main_queue(), ^{ cell.ItemImageView.image = [UIImage imageWithData:feedData]; cell.ItemImageView.layer.masksToBounds = YES; cell.LikeBtn.hidden = NO; cell.CommentBtn.hidden = NO; cell.usernameLabel.hidden = NO; cell.DPImageView.hidden = NO; }); }); [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } else{ //cell.ItemImageView.image = [UIImage imageNamed:@"NoFeed.png"]; cell.LikeBtn.hidden = YES; cell.CommentBtn.hidden = YES; cell.usernameLabel.hidden = YES; cell.DPImageView.hidden = YES; } return cell; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload