原理很简单,一级菜单放在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. ntp-keygen.c

    这个程序产生加密数据文件使用的的密码,遵循Autokey security protocol和NTPv4.文件名被名字和创建时间组成的头部当做前缀,后面跟有一个类型定义的描述符标签和PEM加密的数据结 ...

  2. 使用devcon禁用启用网卡

    系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...

  3. Flex Cairngrom框架浅浅印象

    VO ↓ Model   ←  Delegate ← Command   ↓                                    ↑   ↓                      ...

  4. Android平台设计规范整理(尺寸+组成元素+字体+滑块)

    转自:http://www.ui.cn/project.php?id=12394

  5. UAC绕过思路(未完)

    ---恢复内容开始--- What is UAC?

  6. 最小生成树Prim

    首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...

  7. 使用maven开发过程中,pom报的一些错的解决方法

    1.  maven-resources-plugin prior to 2.4 is not supported by m2e. Use maven-  resources-plugin versio ...

  8. Ubuntu安装完后设置root密码

    安装完Ubuntu 14.04后默认是没有主动设置root密码的,也就无法进入根用户. 相关阅读: Ubuntu 14.04 下载.安装.配置 整理汇总 页面 http://www.linuxidc. ...

  9. ibatis中resultClass与resultMap 的区别

    ibatis的resultClass与resultMap还是有很大的区别.以下是我碰到的一个问题. 配置文件写法如下: 1 sqlMap2 typeAlias alias="notice&q ...

  10. 管理Fragments(转)

    转:原文链接 http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html 管理Fragments FragmentManager 为了管 ...