UITableView 折叠效果
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 折叠效果的更多相关文章
- 实现类似QQ的折叠效果
// 主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. // ViewController.m// 实现类似QQ的折叠效果/ ...
- 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" ...
- 创意设计展示:折叠效果在移动 App 中的应用
在今天在移动 App 界面设计中,你可以看到不同创意类型的视觉效果.特别是在 Dribbble 上面,有有很多应用程序的 UI 概念设计,让你惊叹.当然,他们大多只是作为一个概念设计,可能永远也不会成 ...
- BootStrap入门教程 (四) :JQuery类库插件(模态窗口,滚动监控,标签效果,提示效果,“泡芙”效果,警告区域,折叠效果,旋转木马,输入提示)
上讲回顾:Bootstrap组件丰富同时具有良好可扩展性,能够很好地应用在生产环境.这些组件包括按钮(Button),导航(Navigation),缩略图( thumbnails),提醒(Alert) ...
- Swift 2.0 封装图片折叠效果
文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...
- jquery-练习-折叠效果
<!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- 【转】Android折叠效果实现案例
源文:http://mobile.51cto.com/abased-401983.htm 为了使界面的效果更加绚丽,体验效果更佳,往往需要开发者们自行开发新的界面效果,在这里,我将奉上各种实现折叠效果 ...
随机推荐
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- js set
function ff(){ var sArray = ['a','b','b','b','b','b','d','b']; var iSet = {}; for(var i=0;i<sArra ...
- Computational Geometry Template_Polygon
#include <stdlib.h> #include <math.h> #include <iostream> #define MAXN 1000 #defin ...
- Junit4单元测试
在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 启动tomcat时 一闪而过解决方法
1 首先确定JAVA 已经配好了环境变量,具体配置方法,找一下度娘. 测试方法:进入cmd -> javac -version 能看到JAVA的版本信息,证明配置成功了. 2 分析一下问题出现的 ...
- 网页上的JS call Unity3d里的function——SendMessage
注意: sendmessage只可以从网页发信息到unity游戏里,但是没有返回值 只可以发布三种类型的data,不可以其他复杂的强类型 发信息的时不会做编译检测 SendMessage Workfl ...
- openfire搭建IM
1.openfire 安装配置 在官网上下载,下载最新版本. 在数据库配置方面: 驱动:net.sourceforge.jtds.jdbc.Driver数据库连接:jdbc:jtds:sqlserve ...
- wikioi 1010 过河卒
题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点 ...
- ref和out的使用与区别
out的使用 ————————————————————————————————————————————————— class Program { static void Main( ...
- vs2012 + web api + OData + EF + MYsql 开发及部署
先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发过,因为最近要开发一个手机app的服务端,所以准备用这套框架来开发. 下面开始进入正题( ...