原理很简单,一级菜单放在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. POJ3321/Apple tree/(DFS序+线段树)

    题目链接 Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9692 Accepted: 3217 Descr ...

  2. UVALive 2323 Modular Multiplication of Polynomials(模拟)

    这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题 ...

  3. unity中的update、Lateupdate和FixedUpdate。

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  4. JS页面延迟执行一些方法(整理)

    一般在JS页面延迟执行一些方法.可以使用以下的方法 jQuery.delay()方法简介 http://shawphy.com/2010/11/jquery-delay.html jQuery中que ...

  5. form 表单 enctype 属性-(转自w3c)

    <from action="xxx.xxx" enctype="multipart/form-data"></from> 在上传文件时必 ...

  6. 可信执行环境TEE(转)

    硬件威胁:ARM的架构设计 软件威胁 TEE是中等安全级别 可信执行环境(TEE)是Global Platform(GP)提出的概念.针对移动设备的开放环境,安全问题也越来越受到关注,不仅仅是终端用户 ...

  7. 近期用过的Linux口令备份

    最近使用Ubuntu用到的一些口令: rm -rf directory 移除文件夹或者文件touch (create file)mkdirmv(move,rename)chown (change ow ...

  8. 有关sqlitedrop数据库重建比delete方式来清空数据库更加有效率

    今天浏览stackoverflow 发现一个有趣的问题: which was more preferable as performance wise and without error cause t ...

  9. iOS 旋屏问题

    今天突然想起来,以前的一个问题没有解决,就上网百度了一些方法,看到一篇文章,写的很详细,我就操作试试,结果还真的实现了功能,接下来我将重复他的结合我自己的测试,说一下iOS中的旋屏问题. 1.首先配置 ...

  10. CastelWindsor Demo

    class Program { static void Main(string[] args) { var container = new WindsorContainer(); container. ...