1:创建一个model数据模型

#import <Foundation/Foundation.h>

@interface DataModel : NSObject

//保存section中每行的数据

@property(nonatomic,strong)NSMutableArray *array;

//section名

@property(nonatomic,copy)NSString *name;

//判断section是否展开

@property(nonatomic,assign)BOOL isExpand;

@end

#import "DataModel.h"

@implementation DataModel

-(id)init

{

if (self=[super init]) {

_array=[NSMutableArray array];

}

return self;

}

@end

2:创建一个viewcontroller

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"

#import "DataModel.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

_dataArray=[NSMutableArray array];

for (int i='A'; i<='D'; i++)

{

//设置组名

DataModel *model=[[DataModel alloc]init];

model.name=[NSString stringWithFormat:@"%c",i];

for (int j=0; j<5; j++) {

//设置每组中的行

[model.array addObject:[NSString stringWithFormat:@"%c-%d",i,j]];

}

[_dataArray addObject:model];

}

---PS:我已经在storyboard上面托线条进行关联---

//_myTableView.dataSource=self;

//_myTableView.delegate=self;

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - UITableViewDataSource

#pragma mark -UITableViewDataSource

//返回组数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _dataArray.count;

}

//返回每组对应的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

DataModel *model=_dataArray[section];

if (model.isExpand)

{

return model.array.count;

}

else

{

return 0;

}

}

//每行要显示的值

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier=@"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

DataModel *model=_dataArray[indexPath.section];

cell.textLabel.text=model.array[indexPath.row];

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

//在组名上添加一个按钮

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

//添加一个view存放按钮

UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)];

DataModel *model=_dataArray[section];

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame=CGRectMake(375-15, 5, 15, 8);

btn.tag=section;

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

if(model.isExpand)

{

[ btn setBackgroundImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];

}

else

{

[ btn setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];

}

[contentView addSubview:btn];

if (section%2==0)

{

contentView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

}

else

{

contentView.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

}

return contentView;

}

//通过组名上的按钮来判断是否展开

-(void)btnClick:(UIButton *)sender

{

DataModel *data=_dataArray[sender.tag];

if (data.isExpand)

{

data.isExpand=NO;

}

else

{

data.isExpand=YES;

}

[_myTableView reloadData];

}

@end

3:测试效果图

UITableView  折叠效果的更多相关文章

  1. 实现类似QQ的折叠效果

    //  主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. //  ViewController.m//  实现类似QQ的折叠效果/ ...

  2. css3折叠效果

    在开发过程中,经常会遇到一些交互效果,今天所联系的便是一个类似折纸的折叠效果,查看效果. 说到折纸,我们先看下图 这是我第一时间想到的大体思路,如果能让这6个面连续的变化角度到0不就可以了吗,运用cs ...

  3. jQuery文本段落展开和折叠效果

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  4. jq菜单折叠效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. 创意设计展示:折叠效果在移动 App 中的应用

    在今天在移动 App 界面设计中,你可以看到不同创意类型的视觉效果.特别是在 Dribbble 上面,有有很多应用程序的 UI 概念设计,让你惊叹.当然,他们大多只是作为一个概念设计,可能永远也不会成 ...

  6. BootStrap入门教程 (四) :JQuery类库插件(模态窗口,滚动监控,标签效果,提示效果,“泡芙”效果,警告区域,折叠效果,旋转木马,输入提示)

    上讲回顾:Bootstrap组件丰富同时具有良好可扩展性,能够很好地应用在生产环境.这些组件包括按钮(Button),导航(Navigation),缩略图( thumbnails),提醒(Alert) ...

  7. Swift 2.0 封装图片折叠效果

    文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...

  8. jquery-练习-折叠效果

    <!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  9. 【转】Android折叠效果实现案例

    源文:http://mobile.51cto.com/abased-401983.htm 为了使界面的效果更加绚丽,体验效果更佳,往往需要开发者们自行开发新的界面效果,在这里,我将奉上各种实现折叠效果 ...

随机推荐

  1. C++11外部模板

    [C++11之外部模板] 在标准C++中,只要在编译单元内遇到被完整定义的模板,编译器都必须将其实例化(instantiate).这会大大增加编译时间,特别是模板在许多编译单元内使用相同的参数实例化. ...

  2. ADT-bundle

    eclipse 弹出 Version 1.4.2_03 of the JVM not suitable for this product.Version1.6or geeater is requir ...

  3. commondline 之三 执行jar文件

    java [-options] -jar jarfile [args...] 点击查看获取可执行jar文件方法

  4. 当WEB站点应用程序池标识为ApplicationPoolIdentity,出现运行错误时的解决方法

    对于数据库文件加Authenticated Users用户,并授予完全权限.

  5. Oracle Job相关

    Oracle JOB的建立,定时执行任务      begin            sys.dbms_job.submit(job => :job,                       ...

  6. c\c++ 字符串处理大集合[转]

    rember this strncpy(a,b,); a[]='\0'; ]; memset(a,'#',sizeof(a)); a[]='\0'; 刚开始学C/C++时,一直对字符串处理函数一知半解 ...

  7. angularjs directive学习心得

    一些常见的错误 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-di ...

  8. APK扩展文件及使用

    转自:http://blog.csdn.net/myarrow/article/details/7760579 一.APK扩展文件基本知识 Android Market (Google Play St ...

  9. Uva110 Meta-Loopless Sorts

    Meta-Loopless Sorts Background Sorting holds an important place in computer science. Analyzing and i ...

  10. VS2013 安装phonegap

    https://www.microsoft.com/en-us/download/details.aspx?id=40783