2

所以我想要实现的是当用户从 UICollectionView 中点击一个单元格时,来自这个单元格的图像会显示在下一个 UIView 上。

为了实现这一点,我使用了委托方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath和 NSUserDefaults。这就是我的做法

  1. 敲击单元格
  2. 从 #1 的单元格的 UIImageView 获取图像
  3. 将图像转换为 NSData
  4. 将数据放入 NSUserDefaults
  5. 执行到下一个视图控制器的 segue
  6. 从 NSUserDefaults 获取数据
  7. 转换为 UIImage 并显示在 UIImageView 中。

这是代码:

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

应该工作,但不是。我得到随机结果。有时下一个 UIView 中没有图像,有时有图像但不是我点击的那个。

编辑::这里是实现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;
4

3 回答 3

13

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.

于 2013-11-02T08:34:20.063 回答
2

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];  
}
于 2013-11-02T08:37:08.140 回答
0

也许您已经解决了这个问题,但我遇到了类似的问题,并发现UICollectionView从 0 开始。

例如,我的图像被命名为 image_1、image_2 等等。但如果我从 image_1 开始,第一个单元格中就没有任何内容,所以你必须从 image_0、image_1 等开始。

希望这对任何人都有帮助。

于 2014-05-03T20:18:57.397 回答