我有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
如果触摸一个单元格会导致它崩溃的原因是什么?如果我触摸单元格之间的空间,我可以让它滚动,但所有单元格崩溃都会触摸。
僵尸崩溃的屏幕截图:
答案 0 :(得分:1)
你说你的应用程序会在触摸单元格时崩溃,但你还没有实现单元格选择委托方法,但只写了单元格取消选择委托方法。试试这个
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
在同一方法中插入一个断点进行调试。如果没有调用断点,那么其他问题就是
答案 1 :(得分:1)
您的View Controller类可能存在问题:
@property
ImagePicker
的链接可以是weak
,例如,您可以将其从超级视图中删除。在此之后,ImagePicker
对象将被释放。
尝试替换
@property (weak, nonatomic) ImagePicker *someImagePicker
与
@property (strong, nonatomic) ImagePicker *someImagePicker
您可以分享UIViewController
的代码吗?