触摸单元格时 UICollectionView 崩溃
UICollectionView Crashes when Cell is Touched
我有一个 UICollectionView
,带有自定义 class 子class 到 UICollectionViewCell
,显示应用程序中的图片。它加载正常,但如果任何单元格几乎没有被触及,应用程序就会崩溃。控制台中没有错误消息,如果我放入异常断点也没有任何显示。包含集合视图的视图控制器的代码是:
#import "ImagePicker.h"
#import "CMFGalleryCell.h"
@interface ImagePicker ()
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;
@end
@implementation ImagePicker
@synthesize dataArray, collectionView;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Choose Image";
[self loadImages];
[self setupCollectionView];
// Do any additional setup after loading the view from its nib.
}
-(void)setupCollectionView {
[self.collectionView registerClass:[CMFGalleryCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
[cell setImageName:imageName];
[cell updateCell];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Chose%ld", (long)indexPath.row);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(185, 185);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}
-(void)loadImages {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"inviteimages"];
self.dataArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];
NSLog(@"%@", self.dataArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
自定义 Cell 是:
#import "CMFGalleryCell.h"
@interface CMFGalleryCell()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation CMFGalleryCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CMFGalleryCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
-(void)updateCell {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"inviteimages"];
NSString *filename = [NSString stringWithFormat:@"%@/%@", sourcePath, self.imageName];
UIImage *image = [UIImage imageWithContentsOfFile:filename];
[self.imageView setImage:image];
[self.imageView setContentMode:UIViewContentModeScaleAspectFill];
}
@end
如果触摸单元格,会导致它崩溃的原因是什么?如果我触摸单元格之间的 space,我可以让它滚动,但是所有单元格的任何触摸都会崩溃。
没有僵尸的崩溃截图:
僵尸崩溃截图:
你说你的应用程序会在单元格被触摸时崩溃,但你没有实现单元格 select 委托方法,而是只写了单元格 deselect 委托方法。试试这个
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
并在同一个方法中插入断点进行调试。如果没有调用断点那么就是其他问题了
可能问题出在您的 View Controller class:
@property
link 到 ImagePicker
可以是 weak
,例如,您将其从 superview 中删除。此后,ImagePicker
对象将被释放。
尝试替换
@property (weak, nonatomic) ImagePicker *someImagePicker
和
@property (strong, nonatomic) ImagePicker *someImagePicker
能否分享您 UIViewController
的代码?
我有一个 UICollectionView
,带有自定义 class 子class 到 UICollectionViewCell
,显示应用程序中的图片。它加载正常,但如果任何单元格几乎没有被触及,应用程序就会崩溃。控制台中没有错误消息,如果我放入异常断点也没有任何显示。包含集合视图的视图控制器的代码是:
#import "ImagePicker.h"
#import "CMFGalleryCell.h"
@interface ImagePicker ()
@property (nonatomic, weak) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;
@end
@implementation ImagePicker
@synthesize dataArray, collectionView;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Choose Image";
[self loadImages];
[self setupCollectionView];
// Do any additional setup after loading the view from its nib.
}
-(void)setupCollectionView {
[self.collectionView registerClass:[CMFGalleryCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
[cell setImageName:imageName];
[cell updateCell];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Chose%ld", (long)indexPath.row);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(185, 185);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}
-(void)loadImages {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"inviteimages"];
self.dataArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];
NSLog(@"%@", self.dataArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
自定义 Cell 是:
#import "CMFGalleryCell.h"
@interface CMFGalleryCell()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation CMFGalleryCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CMFGalleryCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
-(void)updateCell {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"inviteimages"];
NSString *filename = [NSString stringWithFormat:@"%@/%@", sourcePath, self.imageName];
UIImage *image = [UIImage imageWithContentsOfFile:filename];
[self.imageView setImage:image];
[self.imageView setContentMode:UIViewContentModeScaleAspectFill];
}
@end
如果触摸单元格,会导致它崩溃的原因是什么?如果我触摸单元格之间的 space,我可以让它滚动,但是所有单元格的任何触摸都会崩溃。
没有僵尸的崩溃截图:
僵尸崩溃截图:
你说你的应用程序会在单元格被触摸时崩溃,但你没有实现单元格 select 委托方法,而是只写了单元格 deselect 委托方法。试试这个
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
并在同一个方法中插入断点进行调试。如果没有调用断点那么就是其他问题了
可能问题出在您的 View Controller class:
@property
link 到 ImagePicker
可以是 weak
,例如,您将其从 superview 中删除。此后,ImagePicker
对象将被释放。
尝试替换
@property (weak, nonatomic) ImagePicker *someImagePicker
和
@property (strong, nonatomic) ImagePicker *someImagePicker
能否分享您 UIViewController
的代码?