原理很简单,一级菜单放在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. 10款免费Bootstrap后台模板演示及下载

    自从有了类似Bootstrap这样强大的前端框架之后,无论我们是做静态页面,还是做网站主题,着实方便很多.即便有很多类似的其他国产.海外的前端框架比较,Bootstrap用户量以及功能文档还是比较大的 ...

  2. tiny210移植linux内核(3.0.8)杂项

    关于三星芯片nand内存分区文件: linux-3.0.8/drivers/mtd/nand/s3c_nand.c struct mtd_partition s3c_partition_info[] ...

  3. 留言本,keyCode

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. Basically Speaking

    Basically Speaking Time Limit: 2 Sec  Memory Limit: 200 MB Submit: 19  Solved: 11 [Submit][Status][W ...

  5. HDU 1010 Temper of the bone(深搜+剪枝)

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  6. cocos2d-x 那些常犯的错误

    Label::_label; if(_label==NULL){ //初始化_label的代码 } //指针默认值不等于NULL,需要赋初始值Label::_label=NULL; string st ...

  7. ios 概况了解

    iOS的系统架构分为四个层次:( iOS是基于UNIX内核,android是基于Linux内核) 核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒 ...

  8. Image File Execution Options(2)

    跳转转到键值HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options在里 ...

  9. SpringMVC中获得HttpRequest对象的方法

    1. 使用@autowired注入HttpRequest 2. 在方法中直接声明形参有HttpRequest即可. 3. 使用一个Listener,然后获取.

  10. CocoaPods安装和使用及问题----看过写的最好的

    目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaPods? 场景1:利用CocoaPods,在项目中导入AFNetworking类库 场景2:如何正确编译运行一 ...