2014-12-30 4 views
1

저는 UICollectionView입니다. 여기 내 MainBundle에서 몇 장의 사진을 보여주고 있습니다. 이 사진의 위치 또는 디렉토리는 SQLite 데이터베이스에 저장됩니다. segue을 사용하고 있습니다. 문제는 이미지 링크를 detailsViewController으로 보내려고하면 링크를 보내지 않는 것입니다.UICollectionViewCell이 다른 viewController에 데이터를 보내고 있습니다.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedPhotoIndex = indexPath.row; 
} 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"]) 
    { 
     PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController]; 
     PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex]; 
     [photoDetailsViewController setImagePath:pProperty.link]; 
     //selectedPhotoIndex = 0; 
    } 
} 

하지만 밖으로 segue.identifier에 코드를 넣어 경우는 detailsViewController에 대한 링크를 보낼 수 있습니다.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController]; 
     PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex]; 
     [photoDetailsViewController setImagePath:pProperty.link]; 
     //selectedPhotoIndex = 0; 
} 

그러나 여기에 문제가

, 그것은 어떤 경우 이전 링크의 참조를 유지하고 내가 선택한 엄지 라인의 이미지를 다시 활용할 수있는 올바른 이미지를 표시 할 수 있습니다. 나는이 대안들을 시도하지만 운은 없다. 하나는 내 문제를 이해하고 모든 솔루션이 나와 함께 공유하시기 바랍니다가있는 경우

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.sQLite = [[SQLite alloc] init]; 
    [self.sQLite callDataBaseAndPhotoTableMethods]; 

    self.arrayOfPhotos = [[NSMutableArray alloc] init]; 
    self.arrayOfPhotos = [self.sQLite returnDataFromPhotoTable]; 

    self.photoCollectionView.delegate = self; 
    self.photoCollectionView.dataSource = self; 
} 

/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/ 
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/ 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [self.arrayOfPhotos count]; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = [self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    self.photoProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row]; 

    // NSLog(@"id %d", photoProperty.idNumPHOTO); 
    // NSLog(@"name %@", photoProperty.name); 
    // NSLog(@"links %@", photoProperty.link); 

    NSData *imageData = [NSData dataWithContentsOfFile:photoProperty.link]; 
    UIImage *originalImage = [[UIImage alloc] initWithData:imageData]; 

    // This is the size where I get from the Cell UIImageView hight & wodth 
    CGSize size = CGSizeMake(36.0f, 51.0f); 
    UIImage *thumbNailImage = [self imageWithImage:originalImage scaledToSize:size]; 

    cell.imageView.image = thumbNailImage; 

    cell.layer.shouldRasterize = YES; 
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

    return cell; 
} 

:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"]) 
    { 
     PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController]; 
     CustomCell *cell = (CustomCell *) sender; 
     NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell]; 

     //NSLog(@"indexPath %@",indexPath); 
     PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row]; 
     [photoDetailsViewController setImagePath:pProperty.link]; 
    } 
} 


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"]) 
    { 
     CustomCell *cell = (CustomCell *) sender; 
     NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell]; 

     int num = indexPath.row %10; 
     NSLog(@"num %i", num); 

     PhotoDetailsViewController *photoDetailsViewController = (PhotoDetailsViewController *)[segue destinationViewController]; 
     PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:num]; 
     [photoDetailsViewController setImagePath:pProperty.link]; 
    } 
} 

여기 내 전체 코드입니다. 고급에 많이 감사드립니다.
좋은 하루 보내십시오.

답변

1

segue의 잘못된 속성을 사용하고있는 것이 문제입니다. performSegueWithIdentifier:에는 identifier이 사용됩니다. destinationViewController을 사용하여 목적지를 얻은 다음이를 확인하십시오. [segue.destinationViewController isKindOfClass:NSClassFromString(@"PhotoDetailsViewController")]

+0

답해 주셔서 감사합니다. 이제 완벽하게 작동합니다. :) – Tulon

0

목적지보기 컨트롤러의 viewDidAppear 메소드에서 imagePath를 확인하십시오.

대상보기 컨트롤러의 첫 번째 viewDidLoad가 호출되고 이전 컨트롤러의 prepareForSegue가 호출됩니다. 따라서 대상보기 컨트롤러의 viewDidLoad에서 imagePath를 사용하는 경우에는 값을 얻지 못합니다.

희망이 도움이 .. ..

+0

의견을 보내 주셔서 감사합니다. :) – Tulon

관련 문제