如何将选定的Collection View Cell的图像推送到另一个View Controller?

时间:2014-02-21 03:06:18

标签: ios iphone objective-c uicollectionviewcell

任何人都可以帮助我...我已经有一个集合视图,显示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

2 个答案:

答案 0 :(得分:1)

您需要使用委托/协议。

将要发送代码的View控制器设置为.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;
}

答案 1 :(得分: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;
    }
}