#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *sectionArray;//section标题

@property(nonatomic, strong)NSMutableArray *rowInSectionArray;//section中的cell个数

@property(nonatomic, strong)NSMutableArray *selectedArray;//是否被点击

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,40 , self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];

_tableView.tableFooterView = [[UIView alloc]init];

_tableView.delegate = self;

_tableView.dataSource = self;

[self.view addSubview:_tableView];

_sectionArray = [NSMutableArray arrayWithObjects:@"第一组",@"第二组",@"第三组",@"第四组", nil];//每个分区的标题

_rowInSectionArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"5",@"6", nil];//每个分区中cell的个数

_selectedArray = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];//这个用于判断展开还是缩回当前section的cell

}

#pragma mark section的个数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _sectionArray.count;

}

#pragma mark cell的行数

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

{

//判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0.

if ([_selectedArray[section] isEqualToString:@"1"]) {

return [_rowInSectionArray[section]integerValue];

}

return 0;

}

#pragma mark cell的内容

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

{

static NSString *identifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

}

cell.textLabel.text = _sectionArray[indexPath.section];

return cell;

}

#pragma cell的高度

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

{

return 40;

}

#pragma mark - section内容

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

{

//每个section上面有一个button,给button一个tag值,用于在点击事件中改变_selectedArray[button.tag - 1000]的值

UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 40)];

sectionView.backgroundColor = [UIColor grayColor];

UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];

sectionButton.frame = sectionView.frame;

[sectionButton setTitle:[_sectionArray objectAtIndex:section] forState:UIControlStateNormal];

[sectionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

sectionButton.tag = 1000 + section;

[sectionView addSubview:sectionButton];

return sectionView;

}

#pragma mark button点击方法

-(void)buttonAction:(UIButton *)button

{

if ([_selectedArray[button.tag - 1000] isEqualToString:@"0"]) {

//        打开这段代码可以实现只展开一个列表的功能

//        for (NSInteger i = 0; i < _sectionArray.count; i++) {

//            [_selectedArray replaceObjectAtIndex:i withObject:@"0"];

//            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];

//        }

//如果当前点击的section是缩回的,那么点击后就需要把它展开,是_selectedArray对应的值为1,这样当前section返回cell的个数就变为真实个数,然后刷新这个section就行了

[_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"1"];

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];

}

else

{

//如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了

[_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];

}

}

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_tableViewDemo1

iOS-tableView点击下拉菜单的更多相关文章

  1. ios学习--iphone 实现下拉菜单

    原文地址:ios学习--iphone 实现下拉菜单作者:sdglyuan00 #import @interface DropDown1 : UIView <</span>UITabl ...

  2. swift 第九课 用tableview 做一个下拉菜单Menu

    写到这里的时候,自己这个项目已经完成了一半左右,项目进度自己还是挺满意.今天又有一个新的布局,要实现个下拉菜单,刚开始写的时候,觉得会很容易,后来发现也是小错不断, 我想自己限制的自己属于写博客的初期 ...

  3. iOS开发 tableView点击下拉扩展 + 内嵌collectionView上传图片效果

    ---恢复内容开始--- //需要的效果 1.设置window的根视图控制器为一个UITableViewController #import "AppDelegate.h"#imp ...

  4. jQuery点击下拉菜单的展示与隐藏

    首先点击显示某个div,然后要求再次点击时消失,或者点击document的其他地方会隐藏掉这个层,涉及到冒泡的问题,阻止document冒泡到dom上.代码如下: var $el = $(" ...

  5. checkbox及css实现点击下拉菜单

    面试遇到的问题.用checkbox中的:checked伪类选择器实现. 通过label标签来触发checkbox的unchecked 和checked两种状态:用css普通同胞选择器 ~.另外补充一点 ...

  6. bootstrap点击下拉菜单没反应

    出现这个问题一般就涉及 网页脚本的问题 好好看看自己网页 scripts 编写是否正确 也可以通过浏览器的 F12 进入console 控制台看看是什么问题 总的来说 该错误要从网页脚本编写的问题出发 ...

  7. js (单个的)点击式下拉菜单

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 关于iview下拉菜单无法添加点击事件的解决办法

    效果如下图所示,点击下拉菜单,点击退出,然后跳到登录界面 代码如下: <Dropdown trigger="click" style="margin-left: 2 ...

  9. JavaScript---网络编程(11)--DHTML技术演示(4)-单选框/下拉菜单/添加文件

    本节讲述单选框/下拉菜单/添加文件,综合css,html和JavaScript. 单选框: 实现的功能是:(类似平时的性格测试) 先隐藏一部分页面,然后通过点击单选框来显示. 再通过选项的选择-(每个 ...

随机推荐

  1. AngularJS的$http服务的应用

    $http有很多参数和调用方法,本文只记录比较常用的应用及参数. $http 服务:只是简单封装了浏览器原生的XMLHttpRequest对象,接收一个参数,这个参数是一个对象,包含了用来生成HTTP ...

  2. attachEvent和addEventListener

    attachEvent和addEventListener在前端开发过程中经常性的使用,他们都可以用来绑定脚本事件,取代在html中写obj.onclick=method. 相同点: 它们都是dom对象 ...

  3. <c:if>标签

    <c:if>的用途就和我们一般在程序中用的if一样. 语法 语法1:没有本体内容(body) <c:if test="testCondition" var=&qu ...

  4. html5 新属性

    <p contenteditable="true">这是一段可编辑的段落.请试着编辑该文本.</p> <input id="email&qu ...

  5. Python 学习日记(第三周)

    知识回顾 在上一周的学习里,我学习了一些学习Python的基础知识下面先简短的回顾一些: 1Python的版本和和安装 Python的版本主要有2.x和3.x两个版本这两个版本在语法等方面有一定的区别 ...

  6. UIAppearance使用详解-备

    iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下 ...

  7. win7 x64 jdk1.7.0_51

    1:我的 jdk与jre默认安装在:D:\Program Files\Java 2:配置环境变量(系统变量): (1)新建JAVA_HOME (2)新建CLASSPATH (3)编辑Path,%JAV ...

  8. c++ 03

    一.面向对象编程 1.什么是对象?什么是对象编程? 1)万物皆对象 2)世界是由一组相互之间紧密联系的对象组成的. 3)通过将对象按照属性和行为共性进行分类,达到将具体事物进行抽象的效果. 4)通过程 ...

  9. linux中的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的 ...

  10. SqlServer 查询表、表说明、关联表、字段说明,语句汇总

    ----查询所有的表 SELECT * FROM SYSOBJECTS WHERE TYPE='U' ----根据表名查询所有的字段名及其注释 SELECT A.NAME,B.VALUE FROM S ...