Reputation: 2489
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.
To implement this I used the delegate method -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
and NSUserDefaults. This is how I am doing it
Here is the 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;
}
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.
EDIT::here is the implementation for cellForItemAtIndexpath
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if(response2.count > 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;
Upvotes: 2
Views: 10366
Reputation: 153
Maybe You've already solved this but I had a similar problem and figured out that UICollectionView
starts at 0.
For example my images were named image_1, image_2 and so on. But if I began with image_1 there wouldn't be anything in the first cell so you would have to start with image_0, image_1 and so on..
Hope this helps anyone.
Upvotes: 0
Reputation: 12023
in didSelectItemAtIndexPath:
method change the line
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NewsfeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; // put this line in place of creating cell
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];
}
Upvotes: 2
Reputation: 1843
Why you do :
NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
? You need to get the cell of the indexPath, not create a cell from the pool. Use :
NewsFeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
Instead.
Upvotes: 13