如何在UICollectionViewSectionHeader中嵌入UICollectionView

时间:2014-08-29 09:10:35

标签: ios objective-c uiviewcontroller uicollectionview uicollectionviewdelegate

由于

,我的应用程序崩溃了
[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 个答案:

答案 0 :(得分:1)

无需将collectionView委托和dataSource设置为UIViewController的子类。对我来说,你不小心将dataSource设置为你的布局,而不是HomeBannerReusableView。检查您设置它的位置(XIB,故事板,代码)。