使用 Parse 数据库的 UICollectionView 问题
Posted
技术标签:
【中文标题】使用 Parse 数据库的 UICollectionView 问题【英文标题】:Problems on UICollectionView with Parse database 【发布时间】:2015-08-30 12:31:54 【问题描述】:我目前正在构建一个 UICollectionView,它显示来自我的解析数据库的照片。 我已经对此主题进行了一些研究,并查找是否有任何教程。我已经按照 YouTube 上的教程给出的说明进行操作:https://www.youtube.com/watch?v=OTXDi0XHsnU 但是当我将 UICollectionView 与 dataSource 和委托连接时,应用程序崩溃了。我想知道是否有关于这个主题的教程以及如何解决这个问题。 我的应用程序是用 object-c 编写的,并且我检查了情节提要中没有重复连接。
相册视图.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
相册视图.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]));
【问题讨论】:
你应该发布崩溃消息! 第一件事是再次确保您在情节提要上使用“imageCell”声明单元重用标识符。 在没有崩溃信息的情况下投票关闭,因为无法解决。 @SanitLee 是的,我已经确认故事板上的标识符是“imageCell” @NilsZiehn 很抱歉。我已经添加了崩溃报告。请帮我解决它。谢谢 【参考方案1】:简而言之,您必须使用 DLImageLoader。
一般example
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData)
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
];
(i) Parse 的演示“PFImage”类本应缓存图像,但实际上并不能正常工作。
(ii) 你可以在网上看到很多关于这个的帖子
(iii) 如果您制作一个处理图像的应用程序,则必须处理缓存。它实际上是“关于”缓存的。
(iv) 如果您不喜欢 DLImageLoader,请使用替代方案(SDWebImage 或其他),或者自己编写,但 DLImageLoader 是最简单和最可靠的。
【讨论】:
我应该将上面的代码替换到哪里?我只是 ios 开发的新手。请帮帮我谢谢 请帮帮我。谢谢。 嗨 KaWing,只需搜索这里的许多示例,例如搜索“解析加载图像”,***.com/a/24346029/294884【参考方案2】:你的代码中有这个方法:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSelection: (NSInteger)selection
return [imageFilesArray count];
但你不是说numberOfItemsInSelection
,你是说numberOfItemsInSection
!!!
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)selection
return [imageFilesArray count];
然后就可以了
【讨论】:
我试过你的方法。但图像加载缓慢。显示一些图像后几秒钟后屏幕突然变黑。这是一个问题还是一个错误。我该如何解决? 我应该用哪一行替换?以上是关于使用 Parse 数据库的 UICollectionView 问题的主要内容,如果未能解决你的问题,请参考以下文章