这两天没事撸撸代码,自己提升一下 就想重构公司的代码 后来想想太多了 就先重构一个界面吧 然后就写了一个CollectionView做的界面 其中遇到的难点就是设置它的headerView然后让他的headerView悬浮于不悬浮的问题
我们先来看看 CollectionView的初始化吧
self.firstView = [[FirstView alloc] initWithFrame:CGRectMake(0, 0, W, H/3)];
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
// flowLayout.itemSize = CGSizeMake(50, 50);
flowLayout.minimumInteritemSpacing = 20;
flowLayout.minimumLineSpacing = 20;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
/**
* 这个非常重要 设置headerView和collectionVirw的层级关系的 也就是设置headerView是否悬浮,
*/
[flowLayout setSectionHeadersPinToVisibleBounds:YES];
/**
* 这个设置headerView的大小的,W(宽度)感觉写与不写没什么关系的 好像默认的是屏幕的宽度
*/
[flowLayout setHeaderReferenceSize:CGSizeMake(W, 200)];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, H - 64) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor grayColor];
self.collectionView.showsVerticalScrollIndicator = NO;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.scrollView addSubview:self.collectionView];
这里感觉collectionView的headerView和tableView的cell差不多,要注册,然后配合这个方法实现
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"asd"];
//设置headerView的方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
_headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"asd" forIndexPath:indexPath];
if (indexPath.section == 1) {
_headerView.backgroundColor = [UIColor yellowColor];
}else{
_headerView.backgroundColor = [UIColor redColor];
}
// [collectionView addSubview:_headerView];
// self.firstView 就是自己定义的headerView 这个如果只需要一个的话 最好提前创建好 不要在这里面创建 否则没走一次方法就会创建一次 容易造成内存负担
[_headerView addSubview:self.firstView];
return _headerView;
}
其余的代理方法就跟正常的一样
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 120;
}
//指定每一个Item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row%2) {
// return CGSizeMake(180, 180);
// }else
return CGSizeMake((W - 60)/2, 260);
//}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * string = @"cell";
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
好吧 我就看了这门多 毕竟我是入门级高手 O(∩_∩)O哈哈~ 如有不对之处 还请大神不吝赐教