层叠选择框的实现(实现方法参照本博客的下拉列表)
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];
}
实现思路如下:
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];
}
相关文章推荐
- jQuery实现在下拉列表选择时获取json数据的方法
- 关于web中下拉列表的几种实现方法
- 下拉列表,选择项的实现方式:
- 实现选择下拉列表中时间而显示图片后台代码
- 关于在GridView模版里添加DropDownList实现根据下拉列表的不同选择gridview根据条件筛选显示。
- JavaScript(JS)实现省市联动选择下拉列表
- 关于web中下拉列表的几种实现方法
- EasyUI中包选择按钮的下拉列表的使用方法
- 实现HTML下拉列表中不能选择的项
- 可编辑Select下拉列表控件实现方法(非DIV模拟)
- javascript实现通过拼音首字母快速选择下拉列表
- 使用each方法实现简单的下拉列表联动
- 使用dwr实现下拉框级联和js实现动态选择option的selected状态方法总结
- PHP下拉框选择的实现方法
- 关于web中下拉列表的几种实现方法
- ajax+jquery+flea+smarty实现了通过选择下拉列表动态显示相应的数据
- 在Excel中实现下拉列表选择录入
- Excel实现下拉列表选择
- javaScript 实现一个 下拉列表选择 的函数
- 数据库中数据添加到下拉列表的实现方法