如下图,这种点击可以展开详细列表的集合视图,下面阐释一种简单的实现方法:
实现思路如下:
A.首先,给集合视图初始化N个分区,给每个分区用一个布尔值参数来判断该分区是否需要展开详细列表,初始的时候,每个分区返回的cell个数是0,即不展开。
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (!_isOpen[section]) {
return0; }
else{
return [[self.allClassArrayobjectAtIndex:section] count];
}}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return4;
}
B.然后,给集合视图返回分区头的方法中添加具有点击手势的UIView,来实现点击该分区头时展开列表的功能。分区头的高度需要设置一下,否则不显示。
//表头(增广视图)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
NSArray *titleArray =@[@"来源",@"状态",@"性别",@"咨询师"];
ClassCollectionReusableView *singerView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"collectionHeader"forIndexPath:indexPath];
singerView.title.text = titleArray[indexPath.section];
UITapGestureRecognizer *alphaTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(TapSection:)];
singerView.tag = indexPath.section +10;
[singerView addGestureRecognizer:alphaTap];
//此处是控制分区头右侧的箭头的图,可忽略
if ( !_isOpen[singerView.tag-10]) {
[singerView.btnsetImage:[UIImageimageNamed:@"buy_jiantou_d@2x"]forState:UIControlStateNormal];
}else{
[singerView.btnsetImage:[UIImageimageNamed:@"buy_jiantou_u@2x"]forState:UIControlStateNormal];
}
return singerView;
}
//增广视图size设置
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
returnCGSizeMake(kWidth -30, 40);
}
- (void)TapSection:(UITapGestureRecognizer *)tap {
//实现功能一:只打开一个分区,其他section隐藏
for (int i=0; i<4; i++) {
_isOpen[i]=0;
}
_isOpen[tap.view.tag-10]=1;
//实现功能二:可以打开多个分区
// _isOpen[tap.view.tag-10]= !_isOpen[tap.view.tag-10];
NSLog(@"TapAction");
//最后刷新集合视图
[adView.selectViewreloadData];
}