CollectionView头视图

#import "oneViewController.h"
#import "MyCellCollectionViewCell.h"
#import 
@interface oneViewController ()
{
    UITableView * table;
    NSArray * arr;
    UICollectionView *collect;
}

@end

@implementation oneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    arr = @[@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10"];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview:table];
    
   
    
    // 创建流布局对象 (保存设定的样式规则)
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    // 设定单元格的大小
    flow.itemSize = CGSizeMake(90, 90);
    // 设定滚动方向
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 最小列间距
    flow.minimumInteritemSpacing = 10;
    // 最小行间距
    flow.minimumLineSpacing = 10;
    // 设定分区的边距
    flow.sectionInset = UIEdgeInsetsMake(10, 25, 120, 10);
    
    
    // 创建网格对象,用流布局对象进行初始化
   collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300) collectionViewLayout:flow];
    // 设定代理关系
    collect.dataSource = self;
    collect.delegate = self;
    
    collect.pagingEnabled = NO;
    collect.backgroundColor = [UIColor whiteColor];
    // 将网格加入到视图上显示
    [self.view addSubview:collect];
    table.tableHeaderView = collect;
    
    // 为网格注册单元格类
    [collect registerClass:[MyCellCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
    //头视图的view,做了适配 手动去设置位置也可以 删掉重新定义就ok
    UIView * v = [[UIView alloc]init];
    v.sd_layout
    .topSpaceToView(self.view, 0)
    .widthIs(self.view.frame.size.width)
    .heightIs(200);
    [self.view addSubview:v];
    
    [collect addSubview:v];
    [table.tableHeaderView addSubview:v];
    
    UILabel * taec = [[UILabel alloc]initWithFrame:CGRectMake(15, 65, 120, 40)];
    taec.text = @"健身达人";
    taec.font = [UIFont systemFontOfSize:18];
    taec.textColor = [UIColor blackColor];
    [self.view addSubview:taec];
    [taec addSubview:v];


    
}
#pragma mark UICollectionViewDelegate
//collection 分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arr.count;
}

// 单元格内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    // 设置显示数据
    cell.img.image = [UIImage imageNamed:arr[indexPath.row]];
    cell.textLab.text = @"叶良辰";
    cell.textfs.text = @"10w粉丝";

    return cell;
    
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
CollectionView头视图_第1张图片
tupian.gif

你可能感兴趣的:(CollectionView头视图)