折叠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双机热备概念
1. 双机热备概述 双机热备有两种实现模式,一种是基于共享的存储设备的方式,另一种是没有共享的存储设备的方式,一般称为纯软件方式. 基于存储共享的双机热备是双机热备的最标准方案. ...
- 转:MFC创建多线程实例
作者:http://blog.csdn.net/wangningyu/article/details/4404134 平时在MFC里使用多线程时其实是很方面的,因为微软提供了一个API让我们很方面的去 ...
- android:onclick属性
android:onclick属性设置点击时从上下文中调用指定的方法,此时给指定一个方法名.例如: xml中: <Button android:layout_width="wrap_c ...
- 【C++基础之十一】虚函数的用法
虚函数的作用和意义,就不进行说明了,这里主要讨论下虚函数的用法. 1.典型的虚函数用法 可以看到,只有标识为virtual的函数才会产生多态的效果,而且是编译多态.它只能借助指针或者引用来达到多态的效 ...
- linux c 之signal 和sigaction区别
http://blog.csdn.net/muge0913/article/details/7331129 要对一个信号进行处理,就需要给出此信号发生时系统所调用的处理函数.可以对一个特定的信号(除去 ...
- linux中断处理程序
Linux进行中断处理的4个步骤: 1.当中断产生,跳到统一入口IRQ_SVC 2.获取中断号 3.根据中断号找到irq_desc结构 4.从irq_desc结构中取出事先注册好的中断处理函数 Lin ...
- 浅谈C#抽象类和C#接口
原文地址:http://www.cnblogs.com/zhxhdean/archive/2011/04/21/2023353.html 一.C#抽象类: C#抽象类是特殊的类,只是不能被实例化:除此 ...
- MVC 模板页和布局
我们在以前的Asp.NET课程中已经学习过母版页了,在MVC中WebForm视图使用母版页的方法与以前基本相同. 创建一个项目MvcMasterPageDemo. 添加Home控制器,生成Index视 ...
- Filebeat中文指南
Filebeat中文指南 翻译自:https://www.elastic.co/guide/en/beats/filebeat/current/index.html 译者:kerwin 鸣谢:tory ...
- SEO学习之路
一.入门建站篇 1.Wamp集成环境安装 2.Wamp集成环境配置多站点 3.DedeCMS安装及目录结构 4.DedeCMS源码安装 5.DedeCMS官方手册 未完待续...