带一个简单头视图的collectionView

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

static NSString *headerViewIdentifier = @"hederview";

@interface AllUseViewController ()

@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

你可能感兴趣的:(带一个简单头视图的collectionView)