1

我的應用程序,因爲如何嵌入UICollectionView內UICollectionViewSectionHeader

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

這是因爲我的委託方法是我UICollectionReusableView內這不是一個視圖控制器崩潰。如何在UICollectionViewSectionHeader中嵌入UICollectionView,並在設置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; 
} 

回答

1

沒有必要將collectionView委託和數據源設置爲UIViewController的子類。在我看來,你不小心將dataSource設置爲您的佈局,而不是HomeBannerReusableView。檢查你設置它的地方(XIB,Storyboard,代碼)。