在storyboard中创建collectionView-添加头

752372-20160426163749798-17047030.png

752372-20160426164237080-462284572.png

collectionView的模拟创建.zip

#import "FindProductNew.h"
#import "SHStoreCollectionCell.h"
#import "HeaderSectionView.h"

@interface FindProductNew ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (weak, nonatomic) IBOutlet UICollectionView *rightCollection; //创建collection

@end

@implementation FindProductNew

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];


#pragma 配置文件
    self.rightCollection.delegate = self;
    self.rightCollection.dataSource = self;
#warning 在storyboard中,不能使用下面的代码,否则cell中的内容都不显示
//    [self.rightCollection registerClass:[SHStoreCollectionCell class] forCellWithReuseIdentifier:@"SHStoreCollectionCell"];
 }

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    HeaderSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderSectionView" forIndexPath:indexPath];
    return headerView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 10;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

#pragma mark 在storyboard中重用identifier与这个要一致
    static  NSString * identifierCell = @"SHStoreCollectionCell";
    SHStoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];

    cell.placeLabel.text = @"张家界";
    cell.backgroundColor = [UIColor whiteColor];

    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

猜你喜欢

转载自blog.csdn.net/github_33467146/article/details/81064581