原理很简单,一级菜单放在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. Tinyxml封装类COperatorXml

    OperatorXml.h头文件 #ifndef _OPERATOR_XML_H_ #define _OPERATOR_XML_H_ #include <string> class TiX ...

  2. oracle的row_number()和rownum

    row_number() 函数和rownum的介绍: 1.row_number() 方法的格式: row_number()over([partition by col1] order by col2) ...

  3. 最短路<dijk>

    题意: 有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路.按格式输出. power oj 2443 题解: 标准dijk算法. #include<c ...

  4. Android ViewDragHelper完全解析 自定义ViewGroup神器

    Android ViewDragHelper完全解析 自定义ViewGroup神器   转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...

  5. 初识Jmeter(一)

    倒霉熊的推荐: 文本学习网址:http://m.open-open.com/m/doc/category/105 视频学习网址: 软件学习网:http://www.ask3.cn/index.html ...

  6. Android中购物车的全选、反选、问题和计算价格

    此Demo主要解决的是购物车中的全选,反选计算价格和选中的条目个数的问题,当选中几条时,点击反选,会把当先选中的变为不选中,把不选中的变为选中.点击全选会全部选中,再次点击时,变为全部不选中. //- ...

  7. APK瘦身

    APK瘦身 主要从一下三方面来瘦身: 1. Java 源代码 1) ,这方面主要是通过最简洁的代码实现最直接的功能,还有就是提出上线前不必要的java代码,可以使用UCDector进行分析,从而对代码 ...

  8. Android消息提示框Toast

    Android消息提示框Toast Toast是Android中一种简易的消息提示框.和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,t ...

  9. zf-监察系统的左侧菜单树的表

    Sys_Right 这个表

  10. hibernate 使用sql 查询(setResultTransformer)

    使用方法举例如下: public List findByOid(Object oid) {  log.debug("finding all WatershedAnalyse instance ...