2014-12-30 85 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) 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]; 
    } 
} 

這裏是我的全碼:

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

如果任何一個理解我的問題,有什麼解決辦法請與我分享。先進的感謝很多。
祝您有愉快的一天。

回答

1

問題是您正在使用segue的錯誤屬性。 identifier用於performSegueWithIdentifier:。您使用destinationViewController來得到您的目的地,並檢查它是這樣的[segue.destinationViewController isKindOfClass:NSClassFromString(@"PhotoDetailsViewController")]

+0

感謝您的回答。它現在正在完美工作。 :) – Tulon

0

檢查您的目標視圖控制器的viewDidAppear方法中的imagePath。

第一個viewDidLoad的目標視圖控制器將被調用,然後prepareForSegue的前一個控制器將被調用。因此,如果您在目標視圖控制器的viewDidLoad中使用了imagePath,那麼您將無法獲得它的值。

希望這會有所幫助..

+0

感謝您的評論。 :) – Tulon

相關問題