1

我的应用程序崩溃是因为

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

这是因为我的委托方法在我的内部UICollectionReusableView,而不是视图控制器。当我为 UICollectionView 设置委托时,如何在 a 中嵌入 UICollectionViewUICollectionViewSectionHeader并防止我的应用程序崩溃。

#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;
}
4

1 回答 1

1

无需将 collectionView 委托和 dataSource 设置为 UIViewController 的子类。在我看来,您不小心将 dataSource 设置为您的布局,而不是 HomeBannerReusableView。检查您设置它的位置(XIB、Storyboard、代码)。

于 2014-08-29T09:21:10.983 回答