实现下拉列表的一种方法
2016-04-13 17:31
465 查看
如下图,这种点击可以展开详细列表的表视图,下面阐释一种简单的实现方法:
实现思路如下:
首先,给表视图初始化N个分区,给每个分区用一个布尔值参数来判断该分区是否需要展开详细列表,初始的时候,每个分区返回的cell个数是0,即不展开。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
_provinceArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger num =
0;
NSArray *citiArray =
_citiesArray[section];
if (!_isOpen[section]) {
num=0;
}else{
num = citiArray.count;
}
return num;
}
然后,给表视图返回分区头的方法中添加具有点击手势的UIView,来实现点击该分区头时展开列表的功能。分区头的高度需要设置一下,否则不显示。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view;
view = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, DEVICE_WIDTH,
44)];
UILabel *titleLabel = [[UILabel
alloc]initWithFrame:CGRectMake(10,
0, 100,
44)];
titleLabel.font = [UIFont
systemFontOfSize:15];
NSString*provinceStr =
_provinceArray[section];
titleLabel.text = provinceStr;
[view addSubview:titleLabel];
view.backgroundColor = [UIColor
whiteColor];
view.tag = section +10;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(ggShouFang:)];
[view addGestureRecognizer:tap];
UIButton *jiantouBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
[jiantouBtn setFrame:CGRectMake(DEVICE_WIDTH-44,
0, 44,
44)];
jiantouBtn.userInteractionEnabled =
NO;
[view addSubview:jiantouBtn];
if ( !_isOpen[view.tag-10])
{
[jiantouBtn setImage:[UIImage
imageNamed:@"buy_jiantou_d.png"]
forState:UIControlStateNormal];
}else{
[jiantouBtn setImage:[UIImage
imageNamed:@"buy_jiantou_u.png"]
forState:UIControlStateNormal];
}
}
return view;
}
最后实现分区头的点击手势的点击方法:点击时对应改变相应分区用于判断是否展开的布尔值参数。刷新tableview或者刷新对应的分区的时候即可展示下拉菜单详情。
-(void)ggShouFang:(UIGestureRecognizer*)ges{
_isOpen[ges.view.tag-10]=!_isOpen[ges.view.tag-10];
NSIndexSet *indexSet=[[NSIndexSet
alloc]initWithIndex:ges.view.tag-10];
[_tabelView
reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationAutomatic];
}
实现思路如下:
首先,给表视图初始化N个分区,给每个分区用一个布尔值参数来判断该分区是否需要展开详细列表,初始的时候,每个分区返回的cell个数是0,即不展开。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
_provinceArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger num =
0;
NSArray *citiArray =
_citiesArray[section];
if (!_isOpen[section]) {
num=0;
}else{
num = citiArray.count;
}
return num;
}
然后,给表视图返回分区头的方法中添加具有点击手势的UIView,来实现点击该分区头时展开列表的功能。分区头的高度需要设置一下,否则不显示。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view;
view = [[UIView
alloc]initWithFrame:CGRectMake(0,
0, DEVICE_WIDTH,
44)];
UILabel *titleLabel = [[UILabel
alloc]initWithFrame:CGRectMake(10,
0, 100,
44)];
titleLabel.font = [UIFont
systemFontOfSize:15];
NSString*provinceStr =
_provinceArray[section];
titleLabel.text = provinceStr;
[view addSubview:titleLabel];
view.backgroundColor = [UIColor
whiteColor];
view.tag = section +10;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(ggShouFang:)];
[view addGestureRecognizer:tap];
UIButton *jiantouBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
[jiantouBtn setFrame:CGRectMake(DEVICE_WIDTH-44,
0, 44,
44)];
jiantouBtn.userInteractionEnabled =
NO;
[view addSubview:jiantouBtn];
if ( !_isOpen[view.tag-10])
{
[jiantouBtn setImage:[UIImage
imageNamed:@"buy_jiantou_d.png"]
forState:UIControlStateNormal];
}else{
[jiantouBtn setImage:[UIImage
imageNamed:@"buy_jiantou_u.png"]
forState:UIControlStateNormal];
}
}
return view;
}
最后实现分区头的点击手势的点击方法:点击时对应改变相应分区用于判断是否展开的布尔值参数。刷新tableview或者刷新对应的分区的时候即可展示下拉菜单详情。
-(void)ggShouFang:(UIGestureRecognizer*)ges{
_isOpen[ges.view.tag-10]=!_isOpen[ges.view.tag-10];
NSIndexSet *indexSet=[[NSIndexSet
alloc]initWithIndex:ges.view.tag-10];
[_tabelView
reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationAutomatic];
}
相关文章推荐
- [二分答案 单调队列] BZOJ 3316 JC loves Mkk
- Java之—— java mail 发送邮件 no object DCH for MIME type multipart/mixed 之正解
- 字符串转换为整数
- 利用eclipse抽取 代码片段为方法
- JS的跨域理解
- JSON入门之二:org.json的基本用法
- css去掉iPhone、iPad默认按钮样式
- oracle 记录
- Java反射机制--肤浅认知
- oracle 记录
- oracle 记录
- oracle 记录
- oracle 记录
- 给定字符的ASCIIn次幂且取膜后的数字,求原来的字符串(快速幂+打表)
- 2016.4.13--css总结
- JS获取节点的兄弟,父级,子级元素
- Centos6.4 + PHP5.5.11 + Mysql5.6.16 + Tnginx2.02 + Memcached1.4.17 + Redis2.2
- bootstrap左侧栏样式
- libevent入门小例子
- 第七周项目1——成员函数、友元函数和一般函数的区别