折叠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 ...
随机推荐
- Eclipse关闭XML文件验证的方法,解决xml警告
XML的编写是否符合规范,可以通过XML Schema或DTD进行验证,但有时候电脑本来就很卡,而且XML的某些错误并未导致程序无法运行的情况下,暂时关闭XML的验证也算不错的选择. 如web.xml ...
- CSS强制图片大小
相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决. 该CSS的功能是:大于600的图片自动调整为600显示. <style type ...
- BufferedReader
Reader FileReader BufferedReader package file; import java.io.BufferedReader; import java.io.File; i ...
- express源码剖析--Router模块
1.加载模块执行代码: methods.forEach(function(method){ //method是http协议的各种请求方法,如:get,put,headee,post Route.pro ...
- 测试Beetle.Redis+Web Api
在Web Api里用Beetle.Redis调用Redis服务端的TIME命令返回一个json格式,然后客户端是用的HttpTest4Net做的测试: CPU:I7-3770 内存:4G+4G 硬盘: ...
- ExpandableListView(可展开的列表组件)的说明以及其用法
ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供,下面是它的xml属性及说明: 然而,接下来是用事实说话了: ...
- 1.Basic Structure
配置: rsyslogd 配置通过rsyslog.conf file,典型的在/etc下.默认的, rsyslogd 读取/etc/rsyslog.conf 文件,这个可以通过命令行选项改变 注意 配 ...
- google官方提供的下拉刷新控件SwipeRefreshLayout
摘自:http://www.stormzhang.com/android/2014/03/29/android-swiperefreshlayout/ SwipeRefreshLayout Swipe ...
- Android手机音量的控制
1.AudioManager audio=(AudioManager) super.getSystemService(Context.AUDIO_SERVICE);//获得手机的声音服务 //设置加音 ...
- html+css显示代码书写版式
由于要显示行数,所以需选用html的列表标签ol,以下代码可以显示代码书写版式的效果: <style> .code-part { background: yellow; } .code-p ...