折叠UITableView分组实现方法
做项目的时侯用到了折叠分组,最近就研究了一下,找了一些网上的项目,发现有一些缺点,研究了几天自己终于写出了一个。而且分组的数据源比较灵活,每组之间的状态没有什么影响。
实现的大体思路是每个分组用一个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分组实现方法的更多相关文章
- [OC][转]UITableView属性及方法大全
Tip: UITableView属性及方法大全 (摘录地址) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 1 ...
- vs 折叠跟展开所有方法。
Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + M + L: 展开所有方法
- iOS开发UITableView基本使用方法总结1
UITableView基本使用方法 1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource 2.然后 ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- UITableView 基本使用方法总结
1..首先,Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource2.然后 UITableView对 ...
- CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏
CASE函数 作用: 可以将查询结果集的某一列的字段值进行替换 它可以生成一个新列 相当于switch...case和 if..else 使用语法: case 表达式/字段 when 值 then ...
- day23_1-re模块之转义字符、分组、方法
#!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...
- python 将值相同的key分组的方法
方法一: 使用 itertools.groupby() rows = [ {'address': '5412 N CLARK ', 'date ': '07/12/2012 ’ }, {'addres ...
随机推荐
- Oracle数据库之事务
Oracle数据库之事务 1. 什么是事务 在数据库中事务是工作的逻辑单元,一个事务是由一个或多个完成一组的相关行为的SQL语句组成,通过事务机制确保这一组SQL语句所作的操作要么都成功执行,完成整个 ...
- javascript时钟
<script language="JavaScript" type="text/javascript"> function now ...
- Ajax 技术原理(转)
Ajax 技术原理 2010-01-04 原文出处:http://www.nowamagic.net/ajax/ajax_AjaxPrinciple.php 在写这篇文章之前,曾经写过一篇关于AJAX ...
- jquery keyup 在IOS设备上输入中文时不触发
今天做一个异步查询功能的时候发现在IOS设备上查询中文时keyup没有触发,在其他设备上时可以的,后来在stackoverflow上找到下面这种解决方法,贴出来算是抛砖引玉了. $h_input.on ...
- ubuntu profile-environment-bashrc 添加环境变量
Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...
- Windows下python安装matplotlib
此文为转载,原文地址为:http://blog.csdn.net/u010585135/article/details/42127273 一.下载matplotlib安装包:网址http://matp ...
- Highly divisible triangular number
我的那个暴力求解,太耗时间了. 用了网上产的什么因式分解,质因数之类的.确实快!还是数学基础不行,只能知道大约. The sequence of triangle numbers is generat ...
- 静态编译Qt5.4.1和Qt WebKit
图灵社区文章地址:http://www.ituring.com.cn/article/195148WebKit是个好东西,做爬虫.显示网页还是想用HTML来做桌面应用的界面都可以用他,不过一直以来都觉 ...
- [Operating System Labs] 我对Linux0.00中 head.s 的理解和注释
?21,# head.s contains the 32-bit startup code.# head.s 是32位的启动代码 # Two L3 task multitasking. The ...
- POJ_2488——骑士遍历棋盘,字典序走法
Description Background The knight is getting bored of seeing the same black and white squares again ...