UICollectionView and push to detailViewController using Parse

断了今生、忘了曾经 提交于 2019-12-24 11:44:50

问题


I use the tapGesture method to pouch the image from UICollectionView to detailViewController viewController.h as follows:

#import <Parse/Parse.h>


@interface CardsViewController : UIViewController <UICollectionViewDelegateFlowLayout> {
    NSMutableArray *allImages;
    NSArray *cardFileArray;
}

@property (weak, nonatomic) IBOutlet UICollectionView *imageCollection

and viewController.m as the follow

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

 }

the detailViewController.h as follow

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

so for the DetailViewController i put in viewDidload this line

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



}

but the app make crash and mark the line on the viewDidload ; so any advice i need to puch the image on uicollectionView to detailView


回答1:


to solve this first you need to identify the image in didselect from parse and declare the segue as well as following

[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];

        }
    }];

then you perform the Segue method and link to the detail view as normal and not the following

link you segue to the "you cell" and the imageview in the cell

Finally, import your cell to the detail view and declare it in your header file (this will be linked to your segue)

i hope this will help you




回答2:


Two things

  1. Remove the tap gesture recognizer and use

    -(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
    
  2. I see you are using storyboards.. just connect the two view controllers by a push segue. Set an UIImage as property of the 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
    }
    

in DetailViewController.h, add

@property (strong,nonatomic) UIImage *imageForDetail;

in DetailViewController.m viewDidLoad add,

self.Preview.image = self.imageForDetail;


来源:https://stackoverflow.com/questions/21393359/uicollectionview-and-push-to-detailviewcontroller-using-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!