#ios #objective-c #uicollectionview
#iOS #objective-c #uicollectionview
Вопрос:
У меня есть UICollectionView, который создает экземпляры множества изображений в макете потока. Проблема, с которой я сталкиваюсь, заключается в том, что при прокрутке до нижней части представления приложение выходит из строя и выдает следующую ошибку: «*** Завершение работы приложения из-за неперехваченного исключения’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
Что может быть причиной этого сбоя?
Комментарии:
1. Почему у вас есть
cell.stickerImage = (UIImageView *)[cell viewWithTag:100];
строка?2. @dan Я не уверен, почему это там, я удалил его, и сбой прекратился, спасибо!
3. Нет необходимости указывать язык как часть заголовка.
4. Проблема не в коде, или, по крайней мере, я ее не нахожу. Кто-то вызывает
.image =
саму ячейку. Установите точку останова исключения и посмотрите, кто это делает.