使用parse uicollectionView并将其推向详细信息视图controller



我使用tapgesture方法将图像从uicollectionview袋装到detail viedViewControllerviewController.h如下:

#import <Parse/Parse.h>

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

和viewController.m作为lonk

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

detail viewcontroller.h如下

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

因此,对于详细信息ViewController,我将其放在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上标记行;因此

要解决此问题,您需要在didSelect中识别parse中的图像并声明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方法并链接到详细信息视图为正常,而不是以下

将您的选择链接到"您的单元"和单元格中的ImageView

最后,将您的单元格导入详细信息并将其声明在您的标题文件中(将其链接到您的SEGUE)

我希望这对您有帮助

两件事

  1. 删除水龙头手势识别器并使用

    -(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
    
  2. 我看到您正在使用故事板。只需通过推送segue连接两个视图控制器。将UIIMAGE设置为详细信息的属性

    -(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
    }
    

详细说明ViewController.h,add

@property (strong,nonatomic) UIImage *imageForDetail;

self.Preview.image = self.imageForDetail;

相关内容

最新更新