原理很简单,一级菜单放在viewForHeaderInSection里面,加一个点击事件,然后判断它的二级菜单(cell)显不显示。

  直接上代码吧!

//

//  HeheTableViewController.m

//  Demo-tableView

//

//  Created by yyt on 16/5/13.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "HeheTableViewController.h"

#define klScreenWidth self.view.bounds.size.width

@interface HeheTableViewController ()

@property(nonatomic,strong) NSArray *dataArr;

@property(nonatomic,strong) NSMutableArray *tapButtons;

@property(nonatomic,strong) NSMutableArray *openedIndexArr;

@end

@implementation HeheTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

_dataArr = @[@{@"name":@"A区",

@"arr":@[@"A1",@"A2",@"A3",@"A4"]

},

@{@"name":@"B区",

@"arr":@[@"B1",@"B2",@"B3"]

},

@{@"name":@"C区",

@"arr":@[@"C1",@"C2",@"C3",@"C4",@"C5",@"C6"]

},

@{@"name":@"D区",

@"arr":@[@"D1",@"D2",@"D3",@"D4",@"D5"]

}

];

_tapButtons = [NSMutableArray array];

_openedIndexArr = [NSMutableArray array];

}

#pragma mark - TableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return _dataArr.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSString *str = [NSString stringWithFormat:@"%ld",section];

if ([_openedIndexArr containsObject:str]) {

NSDictionary *dict = _dataArr[section];

NSArray *arr = dict[@"arr"];

return arr.count;

}

return 0;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

NSString *str = [NSString stringWithFormat:@"%ld",indexPath.section];

if ([_openedIndexArr containsObject:str]) {

NSDictionary *dict = _dataArr[indexPath.section];

NSArray *arr = dict[@"arr"];

cell.textLabel.text = arr[indexPath.row];

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

#pragma mark - TableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

return 50;

}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, klScreenWidth, 50)];

bgView.backgroundColor = [UIColor greenColor];

NSDictionary *dict = _dataArr[section];

NSString *title = dict[@"name"];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, klScreenWidth-40, 30)];

titleLabel.text = title;

titleLabel.textColor = [UIColor whiteColor];

titleLabel.font = [UIFont systemFontOfSize:22];

[bgView addSubview:titleLabel];

UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeCustom];

tapButton.frame = bgView.frame;

[tapButton addTarget:self action:@selector(openOrCloseSectionView:) forControlEvents:UIControlEventTouchUpInside];

[_tapButtons addObject:tapButton];

[bgView addSubview:tapButton];

return bgView;

}

- (void)openOrCloseSectionView:(UIButton*)sender {

NSInteger index = [_tapButtons indexOfObject:sender];

NSString *str = [NSString stringWithFormat:@"%ld",index];

if ([_openedIndexArr containsObject:str]) {

[_openedIndexArr removeObject:str];

} else {

[_openedIndexArr addObject:str];

}

[_tapButtons removeAllObjects];

[self.tableView reloadData];

}

@end

iOS开发——二级列表的更多相关文章

  1. iOS应用内付费(IAP)开发步骤列表

    iOS应用内付费(IAP)开发步骤列表 前两天和服务端同事一起,完成了应用内付费(以下简称IAP, In app purchase)的开发工作.步骤繁多,在此把开发步骤列表整理如下.因为只是步骤列表, ...

  2. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  3. iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)

    iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存 ...

  4. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】

    iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...

  5. iOS开发基础-图片切换(3)之属性列表

    延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...

  6. Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox组件来从列表中选择某一项

    http://blog.csdn.net/delphiteacher/article/details/8924110 Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox ...

  7. iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)

    图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下 ...

  8. iOS开发--开源库

    图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩 ...

  9. iOS开发-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...

随机推荐

  1. jsp环境搭建(Windows)

    1.软件准备 JDK 最新版jdk-8u45-windows-x64.exe tomcat 最新版32-bit/64-bit Windows Service Installer Eclipse IDE ...

  2. linux视频学习4(crontab和进程)

    1 . crontab定时任务: 任务调度: 系统在某个时间执行的特定的命令. 分类: 1.系统工作.2.个别的用户工作. 设置任务调度文件: /etc/crontab 1.crontab -e  : ...

  3. Moya 浅析

    Moya是一个高度抽象的网络库,他的理念是让你不用关心网络请求的底层的实现细节,只用定义你关心的业务.且Moya采用桥接和组合来进行封装(默认桥接了Alamofire),使得Moya非常好扩展,让你不 ...

  4. pat L1-006. 连续因子

    L1-006. 连续因子 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个正整数N的因子中可能存在若干连续的数字.例如630 ...

  5. unit正交相机Size的计算公式

    如:相机的大小为800*480,要使相机适应800*480像素的图,则 Size = 相机高/2/像素单位 = 480/2/100 = 2.4

  6. 屏幕的尺寸(厘米)、屏幕分辨率(像素)、PPI它们之间是什么关系

    屏幕的尺寸(厘米).屏幕分辨率(像素).PPI它们之间是什么关系? 添加评论 分享 赞同2反对,不会显示你的姓名 知乎用户,数据ETL,UNITY3D 刘大侠.如果 赞同 以iphone4 为例,分辨 ...

  7. 英文SEO外部链接资源收集之常用的footprints

      inurl:/privacy-policy "Using Article Directory plugin"inurl:/terms "Using Article D ...

  8. magento 好好玩

    Magento更换服务器的方法   1.把magento的整个目录打包.上传到新服务器,把magento数据库导出,然后在新服务器上导入.如果导不进去的是因为magento的数据库使用了外键约束,通过 ...

  9. centos dmesg

    linux dmesg命令详解   功能说明:显示开机信息. 语 法:dmesg [-cn][-s ] 补充说明:kernel会将开机信息存储在ring buffer,若是开机时来不及查看信息,可利用 ...

  10. 【栈】 poj 1363

    poj1363,这道题是数据结构与算法中的经典问题,给定一组进栈顺序,问栈有多少种出去的顺序. #include<stdio.h> #include <stack> #incl ...