1

My app is crashing because of

[UICollectionViewFlowLayout collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance. 

This is because my delegate methods are inside my UICollectionReusableView which is not a view controller. How can I embed a UICollectionView inside of a UICollectionViewSectionHeader and prevent my app from crashing when I set the delegate for UICollectionView.

#import "HomeBannerReusableView.h"
#import "HomeBannerCell.h"

@interface HomeBannerReusableView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation HomeBannerReusableView

- (void)awakeFromNib {
    // Initialization code
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeBannerCell" bundle:nil] forCellWithReuseIdentifier:@"HomeBannerCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeBannerCellReusableView" forIndexPath:indexPath];
    return cell;
}
Yogendra
  • 1,728
  • 16
  • 28
user1898829
  • 3,437
  • 6
  • 34
  • 62

1 Answers1

1

There is no need to set collectionView delegate and dataSource to subclass of UIViewController. It seems for me that you accidentally set the dataSource to your layout, instead of HomeBannerReusableView. Check the place where you set it (XIB, Storyboard, code).

ksysu
  • 311
  • 4
  • 6