[英]How Can I push the image of selected Collection View Cell to another View Controller?
Can anyone please help me... I already have a collection view that displays all images from a free database on Parse.com. 谁能帮我...我已经有了一个收藏夹视图,该视图可以显示Parse.com上免费数据库中的所有图像。 I need to pass the image of the selected cell to another view controller.
我需要将所选单元格的图像传递给另一个视图控制器。 I've already have the code for prepareForSegue, my problem is that I can't get the value of the selected cell.
我已经有了prepareForSegue的代码,我的问题是我无法获取所选单元格的值。 here's my codes..
这是我的代码。
-(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????
}
}
I've already put codes to viewDidLoad of the recipient view controller 我已经将代码放入收件人视图控制器的viewDidLoad中
You need to make use of Delegate/Protocol. 您需要使用代理/协议。
Set the View controller that you want to send code to as a delegate in your .h file: 将要发送代码的View控制器设置为.h文件中的代理:
@interface NewSubmissionTableViewController : UITableViewController<PhotoPickerViewControllerDelegate>
import the view controller you want to send the image to at the top of your .h file. 在.h文件顶部导入要发送图像的视图控制器。
#import "PhotoPickerViewController.h"
Instantiate the VC you are sending the image too and set a variable within that class equal to the variable you want it to have access to. 还要实例化正在发送图像的VC,并在该类中设置一个变量,使其等于您希望其访问的变量。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
PhotoPickerViewController *photoPicker = segue.destinationViewController;
photoPicker.delegate = self;
photoPicker.selectedPhotos = self.pictureCollection;
}
(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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.