您的位置:首页 > 其它

层叠选择框的实现(实现方法参照本博客的下拉列表)

2016-10-21 09:22 459 查看
如下图,这种点击可以展开详细列表的集合视图,下面阐释一种简单的实现方法:



实现思路如下:

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);

}

C.最后实现分区头的点击手势的点击方法:点击时对应改变相应分区用于判断是否展开的布尔值参数。刷新collectionview或者刷新对应的分区的时候即可展示下拉菜单详情。

- (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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: