UICollectionView を動的に作成しようとしていますが、dataSource またはデリゲートが設定されていない場合に通常発生する例外が引き続き発生します。
* キャッチされない例外 'NSInvalidArgumentException' によりアプリを終了します。理由: '-[UIView collectionView:numberOfItemsInSection:]: 認識されないセレクターがインスタンス 0x8a78ce0 に送信されました'
しかし、それはそこにあります!これは私のコードです:
ヘッダ:
#import <UIKit/UIKit.h>
@interface classHeader : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain) UICollectionView *collectionView;
@end
実装:
#import "classHeader.h"
@implementation classHeader
@synthesize collectionView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection: UICollectionViewScrollDirectionHorizontal];
[flowLayout setItemSize: CGSizeMake(0, 0, 10, 10)];
collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 50, 50) collectionViewLayout:flowLayout];
[collectionView setDelegate:self];
[collectionView setDataSource:self];
[collectionView registerClass:[wbcGuidedAccessManualSlideCell class] forCellWithReuseIdentifier:SlideCellIdentifier];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return 0; // No matter what value is here - exception
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 0; // No matter what value is here - exception
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
return nil; // No matter what value is here - exception
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// TODO: Select Item
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
// TODO: Deselect item
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
- (UIEdgeInsets)collectionView: (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 20, 0, 20);
}
@end
そして 1 つの興味深いメモ:
- ストーリーボードを使用してコネクタを使用して dataSource/delegate を設定する場合、コードで dataSource/delegate を設定する必要があるため、2 回設定しますが、機能します。
- コードのみ、またはストーリーボード コネクタのみを使用すると、機能せず、例外が発生します。
何を設定しなければならないのか、さらに実装する必要があるのか 理解できません..
PSXコード5.0