做项目的时侯用到了折叠分组,最近就研究了一下,找了一些网上的项目,发现有一些缺点,研究了几天自己终于写出了一个。而且分组的数据源比较灵活,每组之间的状态没有什么影响。

实现的大体思路是每个分组用一个section来保存,row0用来保存分组的标题,后面的cell保存每个分组的数据。

1.首先要创建一个保存每组的分组信息的model类,包括分组名,每组里面的cell,和当前组的开关状态。

//MySection.h
#import <Foundation/Foundation.h> @interface MySection : NSObject @property (nonatomic) BOOL isOpen;
@property (nonatomic) NSMutableArray *dataArray;
@property (nonatomic) NSString *name; @end
//MySection.m
#import "MySection.h" @interface MySection () @end @implementation MySection - (instancetype)init
{
self = [super init];
self.isOpen = false;
self.name = @"分组";
self.dataArray = [[NSMutableArray alloc]init];
for (int i = ; i < ; i++) {
NSString *string = [NSString stringWithFormat:@"NO.%i",i]; [self.dataArray addObject:string];
} return self;
} @end

2.通过一个viewController来管理tableView。

//ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @end
//ViewController.m
#import "ViewController.h"
#import "MySection.h" @interface ViewController () @property (nonatomic) UITableView *tableView;
@property (nonatomic) NSIndexPath *selectedIndexPath;
@property (nonatomic) NSMutableArray *sections; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; self.sections = [[NSMutableArray alloc]init];
[self initData]; self.tableView.delegate = self;
self.tableView.dataSource = self; //设置每组之间的距离为0
self.tableView.sectionFooterHeight = ;
self.tableView.sectionHeaderHeight = ; [self.view addSubview:self.tableView]; } //初始化数据
- (void)initData
{
for (int i = ; i < ; i++) {
MySection *section = [[MySection alloc]init];
section.name = [NSString stringWithFormat:@"分组%i",i + ];
[self.sections addObject:section];
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
MySection *theSection = self.sections[section]; //根据分组开关状态和数据源动态改变每组row的个数
if (theSection.isOpen) {
return [theSection.dataArray count];
}else{
return ;
}
} //分组数目
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.sections count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]init]; //从数据源数组中取出当前cell对应的对象
MySection *section = self.sections[indexPath.section]; //如果row为0,则为标题
if (indexPath.row == ) {
cell.textLabel.text = section.name;
cell.backgroundColor = [UIColor grayColor];
}else{
//为每组中cell赋值
cell.textLabel.text = section.dataArray[indexPath.row - ];
} return cell;
} - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MySection *section = self.sections[indexPath.section]; //选中标题cell,且对应的组没有打开
if (!section.isOpen) { NSLog(@"section:%@ open!",section.name);
section.isOpen = YES; NSMutableArray *a = [[NSMutableArray alloc]init]; for (int i = ; i < [section.dataArray count]; i++) {
NSIndexPath *addIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[a addObject:addIndexPath];
} [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:a withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates];
}else if (indexPath.row == ){
//选中的cell对应的组已经打开,且选中的是row0
NSLog(@"section:%@ close!",section.name);
section.isOpen = !section.isOpen; NSMutableArray *b = [[NSMutableArray alloc]init]; for (int i = ; i < [section.dataArray count]; i++) {
NSIndexPath *redIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[b addObject:redIndexPath];
}
[self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:b withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates];
} } //判断是否为标题cell设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != ) {
return ;
}else{
return ;
}
} @end

3.AppDelegate里面没什么特别的了,不过还是贴出来吧。

//AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc]init]; self.window.rootViewController = vc;
self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;
} @end

这样就大功告成了,可以根据情况获取数据源。不过博主也是刚学习了一段时间的菜鸟,有什么错误希望大家指正,共同进步。

折叠UITableView分组实现方法的更多相关文章

  1. [OC][转]UITableView属性及方法大全

    Tip: UITableView属性及方法大全  (摘录地址) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 1 ...

  2. vs 折叠跟展开所有方法。

    Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + M + L: 展开所有方法

  3. iOS开发UITableView基本使用方法总结1

    UITableView基本使用方法 1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource 2.然后 ...

  4. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  5. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  6. UITableView 基本使用方法总结

    1..首先,Controller需要实现两个  delegate ,分别是  UITableViewDelegate 和  UITableViewDataSource2.然后 UITableView对 ...

  7. CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏

    CASE函数   作用: 可以将查询结果集的某一列的字段值进行替换 它可以生成一个新列 相当于switch...case和 if..else 使用语法: case 表达式/字段 when 值 then ...

  8. day23_1-re模块之转义字符、分组、方法

    #!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...

  9. python 将值相同的key分组的方法

    方法一: 使用 itertools.groupby() rows = [ {'address': '5412 N CLARK ', 'date ': '07/12/2012 ’ }, {'addres ...

随机推荐

  1. HTML5 canvas 中的线条样式

    线条样式属性 lineCap        设置或返回线条的结束端点样式 butt         默认.向线条的每个末端添加平直的边缘. round         向线条的每个末端添加圆形线帽. ...

  2. 【Ecstore2.0】计划任务/队列/导入导出 的执行问题

    [环境]CENTOS6.3 + wdcp(php5.3) [症状]可正常加入队列,但不执行队列 [原因]大部份都是用户权限造成 [原理] Ecstore2.0的导入导出.发送邮件.日常清理备份等任务操 ...

  3. WDCP安装memcached

    memcached安装 我们打开Web控制面板的时候其实memcached是没有安装的,所以我们要手动安装这个组件. 首先是要下载安装源代码并执行. 1 2 wget -c http://down.w ...

  4. 安全cookie登录状态设计方案

    我们知道web是基于HTTP协议传输的,明文传输是极其危险的,随便哪个抓包工具分析下数据包,就over啦,一个加密的传输过程应该包括两部分,一部分为身份认证,用户鉴别这个用户的真伪:另外一部分为数据加 ...

  5. 【转】关于FPGA中建立时间和保持时间的探讨

      时钟是整个电路最重要.最特殊的信号,系统内大部分器件的动作都是在时钟的跳变沿上进行, 这就要求时钟信号时延差要非常小, 否则就可能造成时序逻辑状态出错:因而明确FPGA设计中决定系统时钟的因素,尽 ...

  6. Install the Yeoman toolset

    参照:http://yeoman.io/codelab/setup.html 1:$npm install --global yo bower grunt-cli 提示以下错误 npm ERR! /p ...

  7. WPF 自己动手来做安装卸载程序

    原文:WPF 自己动手来做安装卸载程序 前言 说起安装程序,这也许是大家比较遗忘的部分,那么做C/S是小伙伴们,难道你们的程序真的不需要一个炫酷的安装程序么? 声明在先 本文旨在教大家以自己的方式实现 ...

  8. Android Activity整体管理和关闭工具类封装

    如何彻底退出程序,在任意位置退出程序,如何管理当前的运行栈,知道activity的堆栈结构等,本文封装了一个Activity管理类,可以方便随时退出程序. /** * 应用程序Activity管理类: ...

  9. C# winform如何清除由Graphics类绘制出来的所有线条或图形

    在C#winform应用程序中,可以用GDI绘制出线条或图形. 1.在主窗体上绘制线条或图形 using (Graphics g = this.CreateGraphics())      {    ...

  10. Java如何让异常处理机制更完备规范

    1)catch的Exception一定要详细的点名是某种异常而非一概而论的用Exception ex来接收所有的异常,往往不理解这点的人也不能很好的理解catch的意义到底在哪里,是对捕获的异常进行一 ...