UICollectionView dataSource/delegate related excep

2019-07-25 04:10发布

I'm trying to create UICollectionView dynamically but I keep getting an exception that we usually get when dataSource or delegate was not set:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x8a78ce0'

But it’s there! This is my code:

Header:

#import <UIKit/UIKit.h>
@interface classHeader : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, retain) UICollectionView *collectionView;

@end

Implementation:

#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

And one interesting note:

  • if I use storyboard to set up dataSource/delegate using its connectors I still have to set up dataSource/delegate by the code, so I set it twice, but it works;
  • when I use code-only, or storyboard connectors only – it does not work and I get exception.

I can’t understand what I have to set up or to implement more?..

P.S. XCode 5.0

0条回答
登录 后发表回答