2013-11-02 10 views
2

그래서 내가 원하는 것은 사용자가 UICollectionView에서 셀을 탭하면이 셀의 이미지가 다음 UIView에 표시됩니다.UICollectionView의 셀에서 이미지를 가져옵니다.

이것을 구현하기 위해 대표 메서드 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath과 NSUserDefaults를 사용했습니다. 이것은 내가

  1. 그 일을하고있는 세포는
  2. 은 NSUserDefaults
  3. 에 데이터를 넣고있는 NSData로 변환의 # 1
  4. 에서 셀의있는 UIImageView에서 이미지를 이미지를 가져 오기 탭 가져 오기 방법입니다
  5. 다음보기 컨트롤러에 연속 재생 수행
  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]; 

대신.

2

didSelectItemAtIndexPath: 방법에

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

어쩌면 당신은 이미이 문제를 해결 한 줄을 변경하지만 비슷한 문제를 가지고 있었고, 내 이미지 예를 들어 0

에서 UICollectionView 시작이 IMAGE_1 선정됐다 있다고 생각 , image_2 등등. 하지만 image_1로 시작하면 첫 번째 셀에는 아무 것도 없으므로 image_0, image_1 등으로 시작해야합니다.

희망이 있으면 누구나 도움이됩니다.

관련 문제