2013-11-02 49 views
2

所以我想要實現的是,當用戶從UICollectionView中點擊一個單元格時,下一個UIView上顯示來自該單元格的圖像。從UICollectionView中的單元格獲取圖像

爲了實現這個,我使用委託方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath和NSUserDefaults。這是我正在做它

  1. 得到電池輸出
  2. 從細胞的UIImageView的獲取圖像#1
  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; 

回答

13

實施爲什麼這樣做:

NewsfeedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

? 您需要獲取indexPath的單元格,而不是從池中創建單元格。 使用:

NewsFeedCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 

取而代之。在didSelectItemAtIndexPath:方法

2

更改線路

-(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]; 
} 
+0

我有這個事情正在進行,從前一個水龍頭的圖像。如果我第一次點擊圖片(圖片A),我在下一個視圖中什麼都沒有。我回去,然後點擊另一個圖像(圖像B)我得到圖像A從前面的水龍頭 – nupac

+0

可以顯示cellForItemAtIndexPath的實現:方法 – suhit

+0

我很抱歉,對於遲到的響應,但我已經添加了實現 – nupac

0

也許你已經解決了這一點,但我也有類似的問題,想通了,UICollectionView從0開始

例如我的圖片命名IMAGE_1 ,image_2等等。但是,如果我從image_1開始,第一個單元中就不會有任何東西,因此您必須先從image_0,image_1等開始。

希望這有助於任何人。

相關問題