iOS - UICollectionView 的自定义头视图
2017-05-15 16:28
357 查看
这里只自定义区头,区尾类似。方便起见,一个类实现:
#import "ViewController.h"
@interface
ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self
creatUI];
}
#pragma mark - 方法
- (void)creatUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout
alloc]
init];
UICollectionView *collect = [[UICollectionView
alloc] initWithFrame:CGRectMake(0,
20,
self.view.frame.size.width,
self.view.frame.size.height-20)
collectionViewLayout:layout];
[collect registerClass:[UICollectionViewCell
class] forCellWithReuseIdentifier:@"cell"];
/**
* 这里这个Kind必须要用常量字符串,否则会崩溃的不要不要的
*在UICollectionViewFlowLayout.h里面有
UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UICollectionElementKindSectionFooter NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN const CGSize UICollectionViewFlowLayoutAutomaticSize NS_AVAILABLE_IOS(10_0);
*/
[collect registerClass:[UICollectionReusableView
class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"headerID"];
collect.backgroundColor = [UIColor
whiteColor];
collect.delegate =
self;
collect.dataSource =
self;
[self.view
addSubview:collect];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
return
CGSizeMake(self.view.frame.size.width,
150);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return
3;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView =
nil;
if (kind ==
UICollectionElementKindSectionHeader) {
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:@"headerID"
forIndexPath:indexPath];
if (indexPath.section ==
0) {
NSLog(@"这是区头0");
headerView.backgroundColor = [UIColor
redColor];
}else
if (indexPath.section ==
1) {
NSLog(@"这是区头1");
headerView.backgroundColor = [UIColor
greenColor];
}else {
NSLog(@"这是区头2");
headerView.backgroundColor = [UIColor
yellowColor];
}
}
return headerView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
20;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return
CGSizeMake((self.view.frame.size.width-10)/2,
100);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
for (UIView *subView
in cell.contentView.subviews) {
[subView removeFromSuperview];
}
UIView *view0 = [[UIView
alloc] init];
if (indexPath.row%2==0)
{
view0.frame =
CGRectMake(10,
0, (self.view.frame.size.width-10)/2
- 10, 100);
}else {
view0.frame =
CGRectMake(0,
0, (self.view.frame.size.width-10)/2
- 10, 100);
}
view0.backgroundColor = [UIColor
cyanColor];
[cell.contentView
addSubview:view0];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath {
NSLog(@"第%ld个",indexPath.row);
}
最终效果如下截图:
#import "ViewController.h"
@interface
ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self
creatUI];
}
#pragma mark - 方法
- (void)creatUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout
alloc]
init];
UICollectionView *collect = [[UICollectionView
alloc] initWithFrame:CGRectMake(0,
20,
self.view.frame.size.width,
self.view.frame.size.height-20)
collectionViewLayout:layout];
[collect registerClass:[UICollectionViewCell
class] forCellWithReuseIdentifier:@"cell"];
/**
* 这里这个Kind必须要用常量字符串,否则会崩溃的不要不要的
*在UICollectionViewFlowLayout.h里面有
UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UICollectionElementKindSectionFooter NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN const CGSize UICollectionViewFlowLayoutAutomaticSize NS_AVAILABLE_IOS(10_0);
*/
[collect registerClass:[UICollectionReusableView
class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"headerID"];
collect.backgroundColor = [UIColor
whiteColor];
collect.delegate =
self;
collect.dataSource =
self;
[self.view
addSubview:collect];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
return
CGSizeMake(self.view.frame.size.width,
150);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return
3;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView =
nil;
if (kind ==
UICollectionElementKindSectionHeader) {
headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:@"headerID"
forIndexPath:indexPath];
if (indexPath.section ==
0) {
NSLog(@"这是区头0");
headerView.backgroundColor = [UIColor
redColor];
}else
if (indexPath.section ==
1) {
NSLog(@"这是区头1");
headerView.backgroundColor = [UIColor
greenColor];
}else {
NSLog(@"这是区头2");
headerView.backgroundColor = [UIColor
yellowColor];
}
}
return headerView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
20;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return
CGSizeMake((self.view.frame.size.width-10)/2,
100);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
for (UIView *subView
in cell.contentView.subviews) {
[subView removeFromSuperview];
}
UIView *view0 = [[UIView
alloc] init];
if (indexPath.row%2==0)
{
view0.frame =
CGRectMake(10,
0, (self.view.frame.size.width-10)/2
- 10, 100);
}else {
view0.frame =
CGRectMake(0,
0, (self.view.frame.size.width-10)/2
- 10, 100);
}
view0.backgroundColor = [UIColor
cyanColor];
[cell.contentView
addSubview:view0];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath
*)indexPath {
NSLog(@"第%ld个",indexPath.row);
}
最终效果如下截图:
相关文章推荐
- IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView
- IOS之UITableView cell自定义视图中插入Tableview
- 【iOS】UICollectionView自定义Layout之蜂窝布局
- iOS开发-自定义重用机制给ScrollerView添加子视图
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
- iOS开发--UICollectionView网格视图
- IOS中集合视图UICollectionView中Decoration View的简易使用方法。
- iOS自定义alertView,继承自UIView,可以添加子视图,标题图片,文字
- iOS开发 自定义UICollectionViewLayout实现Masonry Layout
- iOS集合视图UICollectionViewCell的选种(高亮)效果
- iOS开发 -- UICollectionView(集合视图)
- IOS瀑布流 通过自定义UICollectionViewController的Layout布局实现
- iOS UITableView表视图(3)自定义cell
- iOS开发 - UICollectionViewLayout 自定义布局
- iOS 6 开发----应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格
- [IOS]swift自定义uicollectionviewcell
- IOSUICollectionView 集合视图
- iOS - 自定义alertView,继承自UIView,可以添加子视图,标题图片+文字
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
- 源码推荐(9.06):collectionView集合视图展开二级视图,可自定义的 iOS 价格日历