【转】 iOS如何实现表格的折叠效果?
原文 : http://blog.csdn.net/youcanping2008/article/details/9202167
一、实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了,就把改组的行设置为0,如果该组隐藏了,就显示该组的所有行。
效果如下:
二、实现步骤
1、定义一个数据模型用于封装数据
#import <Foundation/Foundation.h> @interface MyData : NSObject
{
NSMutableArray *_array;// 每组的数据
BOOL _isShow;// 组的状态,yes显示组,no不显示组
NSString *_name;// 组名
}
@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic,copy) NSString * name;
@property (nonatomic,assign) BOOL isShow;
@end
2、添加数据源
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 全局的数据源
dataArray = [[NSMutableArray alloc] init];
// 添加数据
for (int i='A'; i<='Z'; i++) {
MyData *myData = [[MyData alloc] init];
myData.name = [NSString stringWithFormat:@"%c",i];
for (int j=; j<; j++) {
[myData.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];
}
[dataArray addObject:myData];
}
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , -) style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
myTableView.rowHeight = ;
[self.view addSubview:myTableView];
}
3.实现表格的代理方法
// 组数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [dataArray count];
}
// 根据状态来判断是否显示该组,隐藏组把组的行数设置为0即可
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
MyData *data = [dataArray objectAtIndex:section];
if ([data isShow]) {
return [[data array] count];
}else{
return ;
}
}
// 添加每行显示的内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellName = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName ];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
}
MyData *data = [dataArray objectAtIndex:indexPath.section];
NSString *str = [[data array] objectAtIndex:indexPath.row];
cell.textLabel.text = str;
return cell;
}
4.自定义组的头标题视图,添加点击事件
// 定义头标题的视图,添加点击事件
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
MyData *data = [dataArray objectAtIndex:section]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(, , , );
[btn setTitle:data.name forState:UIControlStateNormal];
btn.tag = section;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
if (section%) {
btn.backgroundColor = [UIColor darkGrayColor];
}else{
btn.backgroundColor = [UIColor lightGrayColor];
}
return btn;
}
- (void) btnClick:(UIButton *)btn
{
MyData *data = [dataArray objectAtIndex:btn.tag];
// 改变组的显示状态
if ([data isShow]) {
[data setIsShow:NO];
}else{
[data setIsShow:YES];
}
// 刷新点击的组标题,动画使用卡片
[myTableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];
}
【转】 iOS如何实现表格的折叠效果?的更多相关文章
- iOS图片折叠效果:Layer的contentsRect属性和渐变层
http://www.cocoachina.com/ios/20150722/12622.html 作者:@吖了个峥 授权本站转载. 前言 此次文章,讲述的是Layer的一个属性contentsRec ...
- jQuery treetable【表格多重折叠树功能及拖放表格子元素重新排列】
今天有个表格需求做到多重折叠子元素功能,仔细想了下实现原理, 1.在html中,把父子节点的关系写在自定义属性,但对于节点是否有孩子(hasChild),是否是最后一个节点(isLastOne),是否 ...
- 创意设计展示:折叠效果在移动 App 中的应用
在今天在移动 App 界面设计中,你可以看到不同创意类型的视觉效果.特别是在 Dribbble 上面,有有很多应用程序的 UI 概念设计,让你惊叹.当然,他们大多只是作为一个概念设计,可能永远也不会成 ...
- css3折叠效果
在开发过程中,经常会遇到一些交互效果,今天所联系的便是一个类似折纸的折叠效果,查看效果. 说到折纸,我们先看下图 这是我第一时间想到的大体思路,如果能让这6个面连续的变化角度到0不就可以了吗,运用cs ...
- jQuery文本段落展开和折叠效果
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- jq菜单折叠效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- BootStrap入门教程 (四) :JQuery类库插件(模态窗口,滚动监控,标签效果,提示效果,“泡芙”效果,警告区域,折叠效果,旋转木马,输入提示)
上讲回顾:Bootstrap组件丰富同时具有良好可扩展性,能够很好地应用在生产环境.这些组件包括按钮(Button),导航(Navigation),缩略图( thumbnails),提醒(Alert) ...
- 实现类似QQ的折叠效果
// 主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. // ViewController.m// 实现类似QQ的折叠效果/ ...
- Swift 2.0 封装图片折叠效果
文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...
随机推荐
- 终端上设置git
http://blog.163.com/xianfuying@126/blog/static/21960005201181482518631/ 在-/.ssh的位置vi id_rsa.pub 拷贝的时 ...
- Android Wear开发 - 数据通讯 - 第一节 : 连接数据层
http://developer.android.com/training/wearables/data-layer/accessing.html Accessing the Wearable Dat ...
- RR区间锁 不是唯一索引,即使区间内没值,也锁
+--------- +---------------------------------------------------------------------------------------- ...
- iostat 使用说明
LINUX [oracle@perass back]$ iostat -m 1 10 Linux 2.6.18-194.el5 (perass) 03/01/2014 avg-cpu: %user % ...
- 【HDOJ】2444 The Accomodation of Students
图论的题目.着色原理+二分图匹配. #include <cstdio> #include <cstring> #define MAXN 205 char map[MAXN][M ...
- MFC添加自定义消息
由于MFC中无法通过类向导来自定义消息,所以需要手动添加,主要过程如下: 本文基于vs2008下通过线程实现数据实时更新的对话框运用程序 1. 定义消息(Resource.h文件中): 由于很多新控件 ...
- poj1743 Musical Theme(后缀数组|后缀自动机)
[题目链接] http://poj.org/problem?id=1743 [题意] 求不可重叠最长重复子串. 2015-11-27 [思路] 1) 据题意处理字符串 ...
- lightoj 1005 组合数学
题目链接:http://lightoj.com/volume_showproblem.php?problem=1005 #include <cstdio> #include <cst ...
- SVN 基本操作
SVN基础 一 简介 tortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理 ...
- 谁动了我的timer?——C#的垃圾回收和调试
先来看如下的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 using System; using System.Threading; publi ...