触摸单元格时 UICollectionView 崩溃
Posted
技术标签:
【中文标题】触摸单元格时 UICollectionView 崩溃【英文标题】:UICollectionView Crashes when Cell is Touched 【发布时间】:2015-08-20 16:35:25 【问题描述】:我有一个UICollectionView
,其中有一个子类为UICollectionViewCell
的自定义类,显示来自应用程序内的图片。它加载正常,但如果任何单元格几乎没有被触摸,应用程序就会崩溃。控制台中没有错误消息,如果我输入异常断点也没有任何显示。包含 Collection View 的视图控制器的代码是:
#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
自定义单元格是:
#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
如果一个单元格被触摸,是什么导致它崩溃?如果我触摸单元格之间的空间,我可以让它滚动,但是所有单元格的任何触摸都会崩溃。
没有僵尸的崩溃截图:
僵尸崩溃截图:
【问题讨论】:
能否请您发布崩溃原因 @T_77 如果您真的阅读了这个问题,您会看到我所说的“控制台中没有错误消息,如果我输入异常断点也没有任何显示” 如果你使用instruments,有时你会得到一个崩溃的原因。尝试使用 Zombie 运行应用程序,看看崩溃时会发生什么 @T_77 从来不知道。做到了,得到了这个 ` *** -[ImagePicker retain]: message sent to deallocated instance 0x7fab6620a020 ` 你能贴一张堆栈跟踪崩溃时的屏幕截图吗? 【参考方案1】:您说当单元格被触摸时您的应用程序会崩溃,但您还没有实现单元格选择委托方法,而是只编写了单元格取消选择委托方法。试试这个
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
并在同一方法内插入断点进行调试。如果没有调用断点,那么问题出在其他地方
【讨论】:
你能发布你的崩溃报告吗?或者你能告诉你的编译器显示的错误信息是什么*** -[ImagePicker retain]: message sent to deallocated instance 0x7fab6620a020
【参考方案2】:
可能问题出在您的 View Controller 类中:
@property
指向ImagePicker
的链接可以是weak
,例如,您可以将其从超级视图中删除。在此之后,ImagePicker
对象将被释放。
尝试替换
@property (weak, nonatomic) ImagePicker *someImagePicker
与
@property (strong, nonatomic) ImagePicker *someImagePicker
你能分享你UIViewController
的代码吗?
【讨论】:
以上是关于触摸单元格时 UICollectionView 崩溃的主要内容,如果未能解决你的问题,请参考以下文章
移除前一个单元格时,UiCollectionView 跳转到下一个单元格
iOS中删除单元格时UICollectionView的动画时间
渲染 UICollectionView 的单元格时不遵守部分插图