Reputation: 593
I'm currently building an UICollectionView which display photos from my parse database. I've make some researches on this topic and find if there are any tutorial. I've followed the instruction give by a tutorial on YouTube: https://www.youtube.com/watch?v=OTXDi0XHsnU but the app crashed when I connected the UICollectionView with dataSource and delegate. I would like to know if there are any tutorial on this topic and how can I fix this problem. My app is written in object-c and I've checked that there are no repeat connection in the storyboard.
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
UPDATE: added crash report
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)
Code that crash the app:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
Upvotes: 0
Views: 101
Reputation: 4331
You have this method in your code:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSelection: (NSInteger)selection {
return [imageFilesArray count];
}
But you dont mean numberOfItemsInSelection
, you mean numberOfItemsInSection
!!!
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)selection {
return [imageFilesArray count];
}
Then it will work
Upvotes: 1
Reputation: 12268
You have to, in short, use DLImageLoader.
General example
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData) {
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
}];
(i) Parse's demo "PFImage" class which supposedly caches images, doesn't really work.
(ii) You can see many posts about this around the net
(iii) If you make an app that handles images, you must deal with caching. It's actually "all about" the caching.
(iv) If you don't prefer DLImageLoader, use an alternative (SDWebImage or whatever), or perhaps write your own, but DLImageLoader is the simplest and most reliable.
Upvotes: 2