UICollectionView并使用Parse推送到detailViewController

我使用tapGesture方法将图像从UICollectionView包装到detailViewController viewController.h中,如下所示:

#import <Parse/Parse.h> @interface CardsViewController : UIViewController <UICollectionViewDelegateFlowLayout> { NSMutableArray *allImages; NSArray *cardFileArray; } @property (weak, nonatomic) IBOutlet UICollectionView *imageCollection 

和viewController.m如下

 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [cardFileArray count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"MyCell"; Cards *cell = (Cards *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; PFObject *imageObject = [cardFileArray objectAtIndex:indexPath.row]; PFFile *imageFile = [imageObject objectForKey:KEY_IMAGE4]; cell.spinController.hidden = NO; [cell.spinController startAnimating]; [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { if (!error) { cell.imageView.image = [UIImage imageWithData:data]; [cell.spinController stopAnimating]; cell.spinController.hidden = YES; } }]; return cell; } - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture{ CGPoint touchPoint = [gesture locationInView:imageCollection]; NSUInteger touchPage = floorf(touchPoint.x / imageCollection.frame.size.width); NSIndexPath *indexPath = [imageCollection indexPathForItemAtPoint:touchPoint]; if (indexPath == nil) { touchPage = touchPage % ([allImages count]); } //Push the image to another view detailViewController*ptvc = [self.storyboard instantiateViewControllerWithIdentifier:@"imageDetail"]; [self.navigationController pushViewController:ptvc animated:YES]; ptvc.imageString = [cardFileArray objectAtIndex:touchedPage]; } 

detailViewController.h如下

 @property (strong, nonatomic) IBOutlet UIImageView *Preview; @property (strong, nonatomic) UIImage *imagePreview; 

所以为DetailViewController我把viewDidload这一行

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //self.imageView.image = [UIImage imageNamed:self.imageViewDetail]; self.Preview.image = self.imagePreview; } 

但该应用程序崩溃并在viewDidload上标记行; 所以任何意见,我需要puch的uicollectionView到detailView的形象

首先要解决这个问题,你需要从parsing中识别didselect中的图像,并声明segue以及以下内容

 [self performSegueWithIdentifier:@"showDetails" sender:imageCollection]; PFObject *object = [cardFileArray objectAtIndex:indexPath.row]; PFFile *file = [object objectForKey:KEY_IMAGE4]; [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { if (!error) { Cards *recipe = [[Cards alloc] init]; recipe.imageView.image = [UIImage imageWithData:data]; } }]; 

那么你执行Segue方法,并像正常的链接到详细视图,而不是以下

链接你segue到“你的细胞”和cellview中的imageview

最后,将你的单元格导入到详细视图中,并在头文件中声明它(这将链接到你的segue)

我希望这能帮到您

两件事情

  1. 删除轻拍手势识别器并使用

     -(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
  2. 我看到你正在使用故事板..只需连接两个视图控制器推推。 将一个UIImage设置为detailViewController的属性

     -(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { //store indexPath.row to a variable accessible outside this function selectedRow = indexPath.row; [self performSegueWithIdentifier:@"detail" sender:nil]; } - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { DetailViewController *detail = [segue destinationViewController]; [detail setImageForDetail:[UIImage imageWithData:[allImages objectAtIndex:selectedRow]]]; //imageArray is the array used to populate the imageCollectionView and i assume they are stored as NSDATA } 

在DetailViewController.h中添加

 @property (strong,nonatomic) UIImage *imageForDetail; 

在DetailViewController.m中添加viewDidLoad,

 self.Preview.image = self.imageForDetail;