2014-02-21 74 views
0

任何人都可以請幫助我...我已經有一個集合視圖,顯示來自Parse.com上免費數據庫的所有圖像。我需要將選定單元格的圖像傳遞給另一個視圖控制器。我已經有了prepareForSegue的代碼,我的問題是我無法獲得所選單元格的值。這裏是我的代碼..如何將所選集合視圖單元的圖像推送到另一個視圖控制器?

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

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


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"imageCell"; 
    GalleryCell *cell = (GalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    PFObject *imageObject = [imageArray objectAtIndex:indexPath.row]; 
    PFFile *imageFile = [imageObject objectForKey:@"parseImage"]; 

    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]]; 

    cell.loadingSpinner.hidden = NO; 
    [cell.loadingSpinner startAnimating]; 

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
     if (!error) { 
      cell.parseImage.image = [UIImage imageWithData:data]; 
      [cell.loadingSpinner stopAnimating]; 
      cell.loadingSpinner.hidden = YES; 
     } 
    }]; 

    return cell; 
} 



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

// should I put anything here? the collection view display images even its blank 

} 

//segue transfer data 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    UINavigationController *nav = [segue destinationViewController]; 
    DetailedVC *transferViewController = (DetailedVC *)nav.topViewController; 

    NSLog(@"prepareForSegue: %@", segue.identifier); 
    if([segue.identifier isEqualToString:@"detailView"]) 
    { 
     //here is my problem... what should I put here???? 
    } 
} 

我已經把代碼viewDidLoad中查看收件人的控制器

回答

0
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self prepareForSegue:<#(UIStoryboardSegue *)#> sender:<#(id)#>] 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UINavigationController *nav = [segue destinationViewController]; 
    DetailedVC *transferViewController = (DetailedVC *)nav.topViewController; 

    NSLog(@"prepareForSegue: %@", segue.identifier); 

    if([segue.identifier isEqualToString:@"detailView"]) 
    { 
     transferViewController.xxx = sender; 
    } 
} 
+0

感謝您的答覆..我來試試 – user3317412

+0

IM在這個行 有錯誤[自prepareForSegue:(UIStoryboardSegue *)發件人:(ID)]; 它說使用未聲明的標識符'發件人' – user3317412

+0

也許你應該在「(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath」獲取單元格,然後將此單元格變爲「GalleryCell」,然後獲取來自細胞的圖像。 –

1

你需要利用代表/協議的。

設置要在你的.h文件發送代碼爲代表的視圖控制器:

@interface NewSubmissionTableViewController : UITableViewController<PhotoPickerViewControllerDelegate> 

進口想將圖像發送給你的.h文件的頂部視圖控制器。

#import "PhotoPickerViewController.h" 

實例化您發送圖像的VC,並在該類中設置一個等於您想要訪問的變量的變量。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    PhotoPickerViewController *photoPicker = segue.destinationViewController; 
    photoPicker.delegate = self; 
    photoPicker.selectedPhotos = self.pictureCollection; 
} 
相關問題