我有一个UICollectionView,可以在Flow Layout中实例化一堆图像。我遇到的问题是,当我滚动到视图底部时,应用程序崩溃并发出以下错误:“***因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:' - [StickerPickerViewCell setImage:]:无法识别的选择器已发送例如0x101bdf470'“。这对我来说似乎很奇怪,因为我不知道为什么当你在底部时会尝试添加更多图像。这是我的代码:
#import <UIKit/UIKit.h>
@interface StickerPickerViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *testText;
@property (weak, nonatomic) IBOutlet UIImageView *stickerImage;
//- (void)setStickerImage:(StickerImage *)sticker;
@end
#import <Foundation/Foundation.h>
#import "StickerPickerViewCell.h"
@implementation StickerPickerViewCell
/*
- (void)setStickerImage:(StickerImage *)sticker{
{
self.stickerImage.image = [UIImage imageNamed:sticker.stickerImage];
}
*/
@end
#import <UIKit/UIKit.h>
#import "PhotoEditViewController.h"
@interface StickerPickerViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
//method declarations
- (void)viewDidLoad;
@property (weak, nonatomic) id<StickerPickerDelegate> pickerDelegate;
@property (weak, nonatomic) IBOutlet UICollectionView *stickerView;// sticker picker collection view
@property (nonatomic, strong) IBOutlet UICollectionViewFlowLayout *flowLayout;
@end
#import <Foundation/Foundation.h>
#import "StickerPickerViewController.h"
#import "StickerPickerViewCell.h"
@implementation StickerPickerViewController
//holds all the locations to the stickers we want to appear
- (void)viewDidLoad {
[super viewDidLoad];
[self.stickerView setBackgroundColor:[UIColor whiteColor]];
//flowlayout is how the cells organize around one another in the collectionview
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
[self.flowLayout setItemSize:CGSizeMake(40, 40)];
[self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.flowLayout.minimumInteritemSpacing = 0.0f;
[self.stickerView setCollectionViewLayout:self.flowLayout];
self.stickerView.bounces = YES;
[self.stickerView setShowsHorizontalScrollIndicator:NO];
[self.stickerView setShowsVerticalScrollIndicator:YES];
}
-(void)viewDidAppear:(BOOL)animated{
}
//////////////////////////////////////////////////COLLECTION VIEW METHODS ///////////////////////////////////////////////////////////////////
//number of cells to produce
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//return list of sticker's size
//printf("i: %lu", (unsigned long)[stickerList count]);
StickerManager* stickerManager = [self.pickerDelegate stickerManagerForStickers];
return stickerManager.stickers.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
StickerManager* stickerManager = [self.pickerDelegate stickerManagerForStickers];
Sticker* sticker = stickerManager.stickers[indexPath.row];
//create cell
StickerPickerViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"stickerPickerViewCell" forIndexPath:indexPath];
//set cell image to image associated with the path in the array
cell.stickerImage.image = sticker.preview;
cell.stickerImage = (UIImageView *)[cell viewWithTag:100];
return cell;
}
//cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(75, 75);
}
//number of cell sections
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//get the selected image as String
Sticker* selectedSticker = [self.pickerDelegate stickerManagerForStickers].stickers[indexPath.row];
[self dismissViewControllerAnimated:YES completion:nil];
[self.pickerDelegate didSelectSticker:selectedSticker];
//dismiss popover
[self dismissViewControllerAnimated:true completion:nil];
}
@end
可能导致此次崩溃的原因是什么?