gpt4 book ai didi

ios - 触摸单元格时 UICollectionView 崩溃

转载 作者:行者123 更新时间:2023-11-28 21:40:50 24 4
gpt4 key购买 nike

我有一个 UICollectionView,其中有一个子类为 UICollectionViewCell 的自定义类,用于显示应用程序中的图片。它加载正常,但如果任何单元格几乎没有被触及,应用程序就会崩溃。控制台中没有错误消息,如果我放入异常断点也没有任何显示。包含 Collection View 的 View Controller 的代码是:

#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

如果触摸单元格,会导致它崩溃的原因是什么?如果我触摸单元格之间的空间,我可以让它滚动,但任何触摸所有单元格都会崩溃。

没有僵尸的崩溃截图: enter image description here

僵尸崩溃截图:

enter image description here

最佳答案

您说您的应用程序会在触摸单元格时崩溃,但您尚未实现单元格选择委托(delegate)方法,而只编写了单元格取消选择委托(delegate)方法。试试这个

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

并在同一个方法中插入断点进行调试。如果没有调用断点,那么就是其他问题

关于ios - 触摸单元格时 UICollectionView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32123681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com