UICollectionView dataSource / delegate相关的异常

时间:2013-10-31 13:21:09

标签: delegates dataset uicollectionview unrecognized-selector

我正在尝试动态创建UICollectionView但是我一直得到一个异常,我们通常在没有设置dataSource或delegate时得到:

*由于未捕获的异常'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

还有一个有趣的说明:

  • 如果我使用storyboard来设置dataSource / delegate使用它的连接器,我仍然需要通过代码设置dataSource / delegate,所以我设置了两次,但它有效;
  • 当我只使用仅代码或故事板连接器时 - 它不起作用,我得到例外。

我无法理解我必须设置或实施更多内容?..

P.S。 XCode 5.0

0 个答案:

没有答案