UICollectionView并使用Parse推送到detailViewController

时间:2014-01-27 22:50:23

标签: ios uicollectionview segue uicollectionviewcell uitapgesturerecognizer

我使用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上标记该行 ;所以任何建议我需要将uicollectionView上的图像提交到detailView

2 个答案:

答案 0 :(得分:2)

首先要解决这个问题,你需要从解析中识别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链接到“you cell”和单元格中的imageview

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

我希望这会对你有所帮助

答案 1 :(得分:0)

两件事

  1. 删除点击手势识别器并使用

    -(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
    
  2. 我看到你正在使用故事板..只需通过push segue连接两个视图控制器。将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
    }
    
  3. 在DetailViewController.h中添加

    @property (strong,nonatomic) UIImage *imageForDetail;
    
    在DetailViewController.m中的

    viewDidLoad add,

    self.Preview.image = self.imageForDetail;