自定义UICollectionView+HeaderView
来源:互联网 发布:淘宝拍下宝贝如何发货 编辑:程序博客网 时间:2024/06/05 02:23
自定义collectionView-header
控制器view初始化view
RecommendGridView* homeGridView = [[RecommendGridView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64-44-49)]; homeGridView.backgroundColor = [UIColor lightGrayColor];[self.view addSubview:homeGridView]; _homeGridView = homeGridView;
RecommendGridView,
自定义collectionView
#import <UIKit/UIKit.h>#import "RecommendHeaderView.h"@interface RecommendGridView : UIView@property (nonatomic, strong) RecommendHeaderView *homeHeaderView;@property (weak, nonatomic) UICollectionView *collectionView;@property (nonatomic, strong) NSMutableArray *productListArray;@end
#import "RecommendGridView.h"#import "HWDConstant.h"#import "UIViewExt.h"#import "HomeCollectionViewCell.h"#import "RecommendHeaderView.h"@interface RecommendGridView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate,UIScrollViewDelegate>@end@implementation RecommendGridView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; [self createCollectionView]; } return self;}- (void)createCollectionView{ if(_collectionView == nil){ UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init]; UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout]; [collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HWDCollectionViewCell"]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.backgroundColor = [UIColor colorWithHexString:@"#eaeaea"]; [self addSubview:collectionView]; _collectionView = collectionView; [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"]; RecommendHeaderView *homeHeaderView = [RecommendHeaderView new]; homeHeaderView.frame = CGRectMake(0, 0, KScreenWidth, 300); _homeHeaderView = homeHeaderView; }}#pragma mark Header- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *reusableView = nil; if (kind == UICollectionElementKindSectionHeader) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath]; [reusableView addSubview:_homeHeaderView]; } return reusableView;}#pragma mark HeaderSize- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(KScreenWidth, _homeHeaderView.height);}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 8;}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"HWDCollectionViewCell"; HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.productModel = _productListArray[indexPath.row]; return cell;}//定义每个Cell的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ CGSize size = CGSizeMake((KScreenWidth)/2-2, 262); return size;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 4;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 4;}@end
headerView
#import <UIKit/UIKit.h>#import "SDCycleScrollView.h"@interface RecommendHeaderView : UIView@property (nonatomic, weak) SDCycleScrollView *cycleScrollView;@property (nonatomic, strong) NSArray *bannerImages;@end
#import "RecommendHeaderView.h"#import "HWDConstant.h"#import "UIViewExt.h"@interface RecommendHeaderView ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>@property (nonatomic, weak) UIButton *buttonSection;@property (nonatomic, weak) UIView * contentView;@property (nonatomic, strong) NSArray<UIImage*> * images;@end@implementation RecommendHeaderView- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; [self setUpHeaderView]; } return self;}- (void)setUpHeaderView{ SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, 160) delegate:self placeholderImage:[UIImage imageNamed:@""]]; //添加标题数组 cycleScrollView.autoScrollTimeInterval = 5; // cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter; cycleScrollView.currentPageDotColor = [UIColor whiteColor]; [self addSubview:cycleScrollView]; _cycleScrollView = cycleScrollView; //设置阴影的颜色 cycleScrollView.layer.shadowColor=[UIColor grayColor].CGColor; //设置阴影的偏移量,如果为正数,则代表为往右边偏移 cycleScrollView.layer.shadowOffset=CGSizeMake(8, 4); //设置阴影的透明度(0~1之间,0表示完全透明) cycleScrollView.layer.shadowOpacity=0.6; //2热门品牌 UILabel * hotTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, cycleScrollView.bottom, KScreenWidth, 35)]; hotTitle.text = @"热门品牌"; hotTitle.textAlignment = NSTextAlignmentCenter; [self addSubview:hotTitle]; //3 UIView * content = [[UIView alloc] initWithFrame:CGRectMake(0, hotTitle.bottom, KScreenWidth, 60)]; content.backgroundColor = [UIColor purpleColor]; [self addSubview:content]; UIView*line = [[UIView alloc] initWithFrame:CGRectMake(0, content.bottom, KScreenWidth, 10)]; [self addSubview:line]; line.backgroundColor = [UIColor groupTableViewBackgroundColor]; //4 300-160-35-60-10 UILabel * beeTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, line.bottom, KScreenWidth, 35)]; beeTitle.text = @"蜜蜂优选"; beeTitle.textAlignment = NSTextAlignmentCenter; [self addSubview:beeTitle];}- (void)setBannerImages:(NSArray *)bannerImages{ _bannerImages = bannerImages; _cycleScrollView.imageURLStringsGroup = bannerImages;}@end
阅读全文
0 0
- 自定义UICollectionView+HeaderView
- UICollectionView的HeaderView、FooterView
- UICollectionView headerView footerView
- UICollectionView添加Section HeaderView
- UICollectionView添加headerview/footerView
- UicollectionView的headerView重叠
- UICollectionView创建头部headerview
- 自定义headerView
- UICollectionView 设置headerView部分悬停
- UICollectionView 为每个section 添加headerview
- 关于UICollectionView的headerView的问题
- UICollectionView headerView悬浮 类似于UItableView的Section
- 如何纯代码给UICollectionView添加HeaderView和FooterView
- IOS-UICollectionView的基本使用以及添加headerView
- 为UICollectionView的每个section设置footerView和headerView
- 95.UICollectionView添加headerView(类似UITableView的tableHeaderView)
- swift中UICollectionView的使用(headerview/footerview/cell/model)
- iOS 11 下 UICollectionView 的HeaderView 遮挡滚动条
- Remote Service使用之IPC
- 【XML】验证器
- js中sort总结
- C语言编程:求n的阶乘问题。输入一个正整数n,输出n!
- Android网络拦截器,支持Debug模式,查看请求与响应的具体信息并修改
- 自定义UICollectionView+HeaderView
- mysql--16310子查询
- github搭建个人博客----------绑定域名访问
- Netty源码解读(四)Netty与Reactor模式
- Android将apk转换成二维码提供给别人扫描并下载
- TabLayout的使用
- 读《文艺复兴三杰,拉斐尔》
- Ubuntu系统初步安装优化
- How to Install Python 3.5.2 on Ubuntu & Linuxmint