使用Parse数据库进行UICollectionView的问题

时间:2015-08-30 12:31:54

标签: ios objective-c parse-platform uicollectionview uicollectionviewcell

我目前正在构建一个UICollectionView,它显示我的解析数据库中的照片。 我已就此主题进行了一些研究,并找出是否有任何教程。我已按照YouTube上的教程提供的说明进行操作:https://www.youtube.com/watch?v=OTXDi0XHsnU但是当我将UICollectionView与dataSource和委托连接时,应用程序崩溃了。我想知道是否有关于此主题的任何教程以及如何解决此问题。 我的应用程序是用object-c编写的,我已经检查过故事板中没有重复连接。

AlbumView.h

#import <UIKit/UIKit.h>
#import "AlbumViewCell.h"
#import <Parse/Parse.h>

@interface AlbumView : UIViewController {

    NSArray *imageFilesArray;
    NSMutableArray *imagesArray;
}

@property (assign, nonatomic) IBOutlet UICollectionView *imagesCollection;
@end

AlbumView.m

#import "AlbumView.h"

@interface AlbumView ()

@end

@implementation AlbumView

@synthesize imagesCollection;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self queryParseMethod];

}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)queryParseMethod {
    NSLog(@"Start query");
    PFQuery *query = [PFQuery queryWithClassName:@"ati8"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            imageFilesArray = [[NSArray alloc] initWithArray:objects];
            NSLog(@"%@", imageFilesArray);

            [imagesCollection reloadData];
        }
    }];
}

-(NSInteger)numberOfSelectionsInCollectionView:(UICollectionView *)
collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSelection: (NSInteger)selection {
    return [imageFilesArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifer = @"imageCell";
    AlbumViewCell *cell = (AlbumViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];

    PFObject *imageObject = [imageFilesArray objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"photos"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            cell.parseImage.image = [UIImage imageWithData:data];
            [imagesCollection reloadData];
        }
    }];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

@end

AlbumViewCell.h

#import <UIKit/UIKit.h>

@interface AlbumViewCell : UICollectionViewCell

@property (assign, nonatomic) IBOutlet UIImageView *parseImage;
@end

AlbumViewCell.m

#import "AlbumViewCell.h"

@implementation AlbumViewCell

@synthesize parseImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Custom initialization
    }
    return self;
}

@end

更新:添加了崩溃报告

2015-08-31 16:31:01.118 HikingHK Online[14345:444090] Start query
2015-08-31 16:31:01.140 HikingHK Online[14345:444090] -[AlbumView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7fce7276d1f0
2015-08-31 16:31:01.147 HikingHK Online[14345:444090] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AlbumView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7fce7276d1f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108937c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001085d0bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010893f0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010889513c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000108894cd8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010938f5fd -[UICollectionViewData _updateItemCounts] + 335
    6   UIKit                               0x0000000109391c09 -[UICollectionViewData numberOfSections] + 22
    7   UIKit                               0x000000010937b771 -[UICollectionViewFlowLayout _getSizingInfos] + 347
    8   UIKit                               0x000000010937c8a9 -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 526
    9   UIKit                               0x00000001093780ed -[UICollectionViewFlowLayout prepareLayout] + 251
    10  UIKit                               0x000000010938fae6 -[UICollectionViewData _prepareToLoadData] + 67
    11  UIKit                               0x00000001093901bf -[UICollectionViewData validateLayoutInRect:] + 54
    12  UIKit                               0x0000000109354850 -[UICollectionView layoutSubviews] + 194
    13  UIKit                               0x0000000108db09eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    14  QuartzCore                          0x00000001073b1ed2 -[CALayer layoutSublayers] + 146
    15  QuartzCore                          0x00000001073a66e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    16  UIKit                               0x0000000108da4635 -[UIView(Hierarchy) layoutBelowIfNeeded] + 607
    17  UIKit                               0x0000000108e9141e -[UINavigationController _layoutViewController:] + 1135
    18  UIKit                               0x0000000108e8e468 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 522
    19  UIKit                               0x0000000108d900f6 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
    20  UIKit                               0x0000000108d8e430 +[UIViewAnimationState popAnimationState] + 289
    21  UIKit                               0x0000000109095036 -[UINavigationTransitionView transition:fromView:toView:] + 2569
    22  UIKit                               0x0000000108e92170 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2984
    23  UIKit                               0x0000000108e92408 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
    24  UIKit                               0x0000000108e92ece -[UINavigationController __viewWillLayoutSubviews] + 43
    25  UIKit                               0x0000000108fdd6d5 -[UILayoutContainerView layoutSubviews] + 202
    26  UIKit                               0x0000000108db09eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    27  QuartzCore                          0x00000001073b1ed2 -[CALayer layoutSublayers] + 146
    28  QuartzCore                          0x00000001073a66e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    29  QuartzCore                          0x00000001073a6556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    30  QuartzCore                          0x000000010731286e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    31  QuartzCore                          0x0000000107313a22 _ZN2CA11Transaction6commitEv + 462
    32  UIKit                               0x0000000108d2e9ed -[UIApplication _reportMainSceneUpdateFinished:] + 44
    33  UIKit                               0x0000000108d2f6b1 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2648
    34  UIKit                               0x0000000108d2e095 -[UIApplication workspaceDidEndTransaction:] + 179
    35  FrontBoardServices                  0x000000010e09b5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
    36  CoreFoundation                      0x000000010886b41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    37  CoreFoundation                      0x0000000108861165 __CFRunLoopDoBlocks + 341
    38  CoreFoundation                      0x0000000108860947 __CFRunLoopRun + 887
    39  CoreFoundation                      0x0000000108860366 CFRunLoopRunSpecific + 470
    40  UIKit                               0x0000000108d2db02 -[UIApplication _run] + 413
    41  UIKit                               0x0000000108d308c0 UIApplicationMain + 1282
    42  HikingHK Online                     0x0000000106c273e3 main + 99
    43  libdyld.dylib                       0x000000010dc6f145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

导致应用崩溃的代码:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

2 个答案:

答案 0 :(得分:2)

简而言之,您必须使用DLImageLoader。

一般example

[DLImageLoader loadImageFromURL:imageURL
           completed:^(NSError *error, NSData *imgData) {
           imageView.image = [UIImage imageWithData:imgData];
        [imageView setContentMode:UIViewContentModeCenter];
    }];

(i)Parse的演示&#34; PFImage&#34;据说可以缓存图像的类,并不能真正起作用。

(ii)你可以在网上看到很多关于此事的帖子

(iii)如果您制作处理图片的应用,则必须处理缓存。它实际上是关于&#34;所有关于&#34;缓存。

(iv)如果您不喜欢DLImageLoader,请使用替代方案(SDWebImage或其他),或者编写您自己的替代方案,但DLImageLoader是最简单和最可靠的。

答案 1 :(得分:1)

您的代码中有这种方法:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSelection: (NSInteger)selection {
    return [imageFilesArray count];
}

但你不是指numberOfItemsInSelection,你的意思是numberOfItemsInSection !!!

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)selection {
    return [imageFilesArray count];
}

然后它会起作用