本篇文章主要介绍了iOS中的二级菜单及Cell的展开收起示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
最近又做了一个项目,涉及到二级菜单及cell的展开收起,这是我所做过的第三个项目中做这个功能了,我当然不能把公司的项目界面show出来,所以我重新创建一个工程,数据都写的是固定的数据。作为总结,记录实现过程,及要注意的一些点:如进来默认选中第一行,数据优化等。
先看看我们实现的效果:
//默认选中第一行
NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
[leftTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
(4)在tableView didSelectRowAtIndexPath 方法中,根据点击的左边的cell,请求右边的数据。我们不能每次点击都请求一次,这样很耗费用户的流量。
我们需要把右边的数据放在可变数组里arr,全部初始化arr = [NSMutable array];,每次点击,先判断arr.count ==0 ,如果!=0 再去请求数据,然后reload data。
2.cell的弹开和收起
在效果图中可以看到点击tableview的区的headerview,对应区的row会弹开收起。
(1)我们在获取数据的时候,创建一个数组,给每个区的headerview一个标志“0”,即默认为收起
//specificaArr是效果图中左边的cell英国,对应的右边的数据源
//flagArr是左边对每个区的标识
for (int i = 0; i<specificArr.count; i++) {
[flagArr addObject:@"0"];
}
(2)给headerview添加一个手势,且给headerview一个tag值方便在手势响应事件中知道我们具体点击的是哪个区
view.tag = 100+section;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionClick:)];
[view addGestureRecognizer:tap];
(3)在手势响应事件中根据headerview的标识选择展开还是收起row,且改变标识
-(void)sectionClick:(UITapGestureRecognizer *)tap{
//根据tag值获取点击的区
int index = tap.view.tag%100;
//创建可变数据,存储所点击的区的所有行的indexpath,tableview刷新区对应的行,重新设置行高
NSMutableArray *indexArray = [[NSMutableArray alloc]init];
NSArray *arr = specificArr[index];
for (int i = 0; i<arr.count; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:index];
[indexArray addObject:path];
}
//展开
if ([flagArr[index] isEqualToString:@"0"]) {
[flagArr replaceObjectAtIndex:index withObject:@"1"];
[specificTable reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom];
}else{
[flagArr replaceObjectAtIndex:index withObject:@"0"];
[specificTable reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom];
}
}
(4)在tableView heightForRowAtIndexPath方法中设置tableview的高度
if ([flagArr[indexPath.section] isEqualToString:@"0"]) {
return 0;
}else{
return 96;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
本文标题为:iOS中的二级菜单及Cell的展开收起示例
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android实现轮询的三种方式 2023-02-17
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Android studio实现动态背景页面 2023-05-23
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Flutter实现底部和顶部导航栏 2022-08-31
- Android实现监听音量的变化 2023-03-30
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
