带一个简单头视图的collectionView

#import "AllUseViewController.h"
#import "AllUseCollectionViewCell.h"

static NSString *headerViewIdentifier = @"hederview";

@interface AllUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *headerView;

@end

@implementation AllUseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.naviTitle = @"全部应用";
    self.view.backgroundColor = COLOR_TABLE;
    
    [self configUI];
}

-(void)configUI
{
    // 如不需要长按排序效果,将LxGridViewFlowLayout类改成UICollectionViewFlowLayout即可
    UICollectionViewFlowLayout * layOut = [[UICollectionViewFlowLayout alloc] init];
    layOut.minimumLineSpacing = 0;
    layOut.minimumInteritemSpacing = 0;
    CGFloat W = (DEVICE_SCREEN_WIDTH - 20 ) / 4;
    layOut.itemSize = CGSizeMake(W, 160);
    layOut.headerReferenceSize=CGSizeMake(DEVICE_SCREEN_WIDTH - 20, 44); //设置collectionView头视图的大小
    
    //collectionView
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 0, DEVICE_SCREEN_WIDTH - 20, DEVICE_SCREEN_HEIGHT - DEVICE_NAVIGATION_BAR_HEIGHT - DEVICE_STATUS_BAR_HEIGHT) collectionViewLayout:layOut];
//    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    //注册cell单元格
    [_collectionView registerNib:[UINib nibWithNibName:@"AllUseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"AllUseCollectionViewCell"];
    //注册头视图
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
    [self.view addSubview:_collectionView];
}


#pragma mark - CollectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    AllUseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllUseCollectionViewCell" forIndexPath:indexPath];
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
//  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
        //添加头视图的内容
        [self addContent];
        //头视图添加view
        [header addSubview:self.headerView];
        return header;
    }
    //如果底部视图
    //    if([kind isEqualToString:UICollectionElementKindSectionFooter]){
    //
    //    }
    return nil;
}
/*
 *  补充头部内容
 */
-(void)addContent
{
    UIView *headerView=[[UIView alloc]init];
    headerView.backgroundColor = COLOR_TABLE;
    headerView.frame=CGRectMake(0, 0, DEVICE_SCREEN_WIDTH - 20, 44);
    
    UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(10, (headerView.frame.size.height - 13) / 2, 3, 13)];
    yellowView.backgroundColor = COLOR_HEX(0xFF9D2D, 1.0);
    [headerView addSubview:yellowView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, 0, DEVICE_SCREEN_WIDTH - 20 - 33, headerView.frame.size.height)];
    titleLabel.font = [UIFont systemFontOfSize:12.0];
    titleLabel.text = @"户口办理";
    titleLabel.textColor = COLOR_HEX(0x989898, 1.0);
    [headerView addSubview:titleLabel];
    
    self.headerView=headerView;
}
@end
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #import "ViewController.h" @interface ViewController (){ ...
    Willism阅读 1,040评论 0 1
  • 最近看了一本书,叫《疾病心理学》,里面讲了一些疾病根源和自我治疗的相关理论,玄玄乎乎的,我刚看了一半,挺感兴趣,可...
    chajn阅读 2,032评论 0 2
  • 已是深春,花开了,柳树开始飘絮,在阿漾住处的后头又一方不大不小的池塘,名为随缘湖,有好几十年的历史了。 放假了,阿...
    沈还乡阅读 301评论 2 1
  • 高情商父母成长营,家长总共十八次课。陪伴孩子让孩子成为更好的自己。 卓越性词汇给人带来正能量。互动~两两互相说卓越...
    沁林流水阅读 390评论 0 0
  • 今天是我第五天当助教,我再一次收获满满! 上午听老师跟我们讲了新昌的学习是很死板的,原来我是我想要...
    姚yao佳阅读 274评论 0 0