实现可折叠的分组tableview
http://superman474.blog.163.com/blog/static/120661462011468242287/
运行效果如下,分别是折叠状态的tabview和展开状态的tabview:
?
一、新建UITableViewController
.h文件如下,包含了一个用于显示的视图tableview和用于表示模型数据的MutableArray.
@interface GDXXDetailVC :UITableViewController
<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>
{
UITableView* tableView;
NSMutableArray* model;
UIBarButtonItem *btnSave;
NSString *account,*pass;
NSArray* keys;
}
-(void)setModel:(NSString*)_account pass:(NSString*)_pass data:(NSArray*)_data;
-(void)save;
-(void)collapseOrExpand:(int)section;
-(Boolean)isExpanded:(int)section;
@end
.m文件如下,包含了tableview的datasource方法,和模型的处理逻辑。
#import "GDXXDetailVC.h"
@implementation GDXXDetailVC
-(id)init{
if(self=[super init]){
self.title=@"工单处理";
}
return self;
}
-(void)setModel:(NSString*)_account pass:(NSString*)_pass data:(NSArray*)_data
{
account=_account;
pass=_pass;
model=[[NSMutableArray alloc]init];
[model setArray:_data];
[_data release];
}
-(void)loadView{
self.view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 480) style:UITableViewStyleGrouped];
[self.view addSubview:tableView];
tableView.delegate=self;
tableView.dataSource=self;
//这个图片中工具栏中显示一个保存按钮
btnSave= [[UIBarButtonItem alloc]
initWithTitle:@"处理"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(save)];
self.navigationItem.rightBarButtonItem = btnSave;
[btnSave release];
}
-(void)saveData{
}
#pragma mark Actionsheet 委托方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{//当ActionSheet的某个按钮被按下时触发
if(buttonIndex == 0)//第一个按钮表示保存按钮
{
[self performSelector:@selector(saveData)];
}
//解散actionSheet
[actionSheet dismissWithClickedButtonIndex: buttonIndex animated:YES];
}
#pragma mark ===table view dataSource method and delegate method===
//返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [model count];
}
//返回组标题
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// NSDictionary* d=[model objectAtIndex:section];
// if(d!=nil)
//title=[d objectForKey:@"title"];
// else return nil;
//}
//自定义section header
- (UIView *) tableView: (UITableView *) tableView
viewForHeaderInSection: (NSInteger) section
{
NSString*title=@"notitle";
NSDictionary* d=[model objectAtIndex:section];
if(d!=nil)
title=[d objectForKey:@"title"];
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
footerView.autoresizesSubviews = YES;
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
footerView.userInteractionEnabled = YES;
footerView.hidden = NO;
footerView.multipleTouchEnabled = NO;
footerView.opaque = NO;
footerView.contentMode = UIViewContentModeScaleToFill;
// Add the label
UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 5, 120.0, 45.0)];
footerLabel.backgroundColor = [UIColor clearColor];
footerLabel.opaque = NO;
footerLabel.text = title;
footerLabel.textColor = [UIColor blackColor];
footerLabel.highlightedTextColor = [UIColor blueColor];
footerLabel.font = [UIFont boldSystemFontOfSize:17];
footerLabel.shadowColor = [UIColor whiteColor];
footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
[footerView addSubview: footerLabel];
[footerLabel release];
// Add the button
UIButton* footerButton = [[UIButton alloc] initWithFrame:CGRectMake(12, 5, 48.0, 48.0)];
//一开始小节是处于“折叠状态”,“+/-”按钮显示“+号”图标
if ([self isExpanded:section]) {//若本节转换到“展开”状态,需要把图标显示成“-”号
[footerButton setBackgroundImage:[UIImage imageNamed:@"minus.png"] forState:UIControlStateNormal];
}else
[footerButton setBackgroundImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
[footerButton addTarget:self action:@selector(expandButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
footerButton.tag=section;//把节号保存到按钮tag,以便传递到expandButtonClicked方法
[footerView addSubview: footerButton];
[footerButton release];
// Return the footerView
return footerView;
}
//当“+/-”按钮被点击时触发
-(void)expandButtonClicked:(id)sender{
UIButton* btn=(UIButton*)sender;
int section=btn.tag; //取得节号
[self collapseOrExpand:section];
//刷新tableview
[tableView reloadData];
}
//对指定的节进行“展开/折叠”操作
-(void)collapseOrExpand:(int)section{
Boolean expanded=NO;
NSMutableDictionary* d=[model objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
//若原来是折叠的则展开,若原来是展开的则折叠
[d setObject:[NSNumber numberWithBool:!expanded] forKey:@"expanded"];
}
//返回指定节的“expanded”值
-(Boolean)isExpanded:(int)section{
Boolean expanded=NO;
NSMutableDictionary* d=[model objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
return expanded;
}
// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 60;
}
//返回分组的行数
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
//对指定节进行“展开”判断
if (![self isExpanded:section]) {//若本节是“折叠”的,其行数返回为0
return 0;
}
NSDictionary* d=[model objectAtIndex:section];
return [[d objectForKey:@"items"] count];
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
//设置每一单元格的内容
-(UITableViewCell*)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString*cellId=@"setcell";
UITableViewCell* cell=(UITableViewCell*)[table dequeueReusableCellWithIdentifier:cellId];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId]autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
NSDictionary* items=[[model objectAtIndex:indexPath.section] objectForKey:@"items"];
keys=[items allKeys];
cell.textLabel.text=[items objectForKey:[keys objectAtIndex:indexPath.row]];
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:18.0];
return cell;
}
//单元格选中时触发
-(void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
-(void)save{
}
-(void)dealloc{
[model release];
[tableView release];
[super dealloc];
}
@end
二、在application的AppDelegate中实例化TableViewController
在application方法中,构造好一个Array,把要展示的数据放到其中,然后调用TableViewController的setModel方法设置tableview的model。这个Array的结构应该是这样的:
NSArray 中的元素为NSMutableDictionary(必须是Mutable,不能是NSDictionary)。每一个 NSMutableDictionary代表了一个小节的数据,包含若干key-value,其中Title为小节名称,expanded为小节的展开/ 折叠状态,items为小节中每一行的数据。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
GDXXDetailVC* rootController=[[GDXXDetailVC alloc]init];
NSMutableArray* items=[[NSMutableArray alloc]init];
for (int i=1; i<10; i++) {
NSDictionary *d=[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"section %d item1",i],@"1",
[NSString stringWithFormat:@"section %d item2",i],@"2",
[NSString stringWithFormat:@"section %d item3",i],@"3",
nil];
NSMutableDictionary* dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"title %d",i],@"title",
d,@"items",[NSNumber numberWithBool:NO],@"expanded",
nil];
//[d release];
[items addObject:dic];
//[dic release];
}
[rootController setModel:nil pass:nil data:items];
//[items release];
[rootController setTitle:@"无线应用"];
[window addSubview:rootController.view];
//[rootController release];
[window makeKeyAndVisible];
转载于:https://www.cnblogs.com/pengyingh/articles/2355418.html
实现可折叠的分组tableview相关推荐
- java 下拉列表 可折叠 qq分组_2.5.5 ExpandableListView(可折叠列表)的基本使用
本节引言: 本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含 ...
- java 下拉列表 可折叠 qq分组_Java在Excel中创建多级分组、折叠或展开分组的实现...
本文介绍通过Java程序在Excel创建分组的方法,可对行或列分组进行分组并设置明细数据是否展开或折叠.设置数据分组并展开或折叠时,可通过以下方法: 方法一: 通过方法sheet.groupByRow ...
- java 下拉列表 可折叠 qq分组_Java 在Excel中创建多级分组、折叠或展开分组
以下经验内容分享通过Java程序在Excel中创建多级分组.折叠或展开分组. 可对行或列分组进行分组并设置明细数据是否展开或折叠.设置数据分组并展开或折叠时,可通过以下方法: 方法一: 通过方法she ...
- CSDN上Android与IPhone开发博客精选
Android [1] 张国威:Android从入门到提高系列 前面写了十四篇关于界面的入门文章,大家都看完和跟着练习之后,对于常用的Layout和View都会有一定的了解了,接下来的文章就不再强调介 ...
- UITableView的折叠收缩和QQ好友分组效果
可折叠展开的tableView,QQ好友分组列表 demo下载地址https://github.com/zhengwenming/ExpandTableView 原理分析:这个可以折叠的table ...
- iOS 项目源码大全 github 国内外大神
github排名https://github.com/trending,github搜索:https://github.com/search 主要工作说明: 重新整理了Xcode好用的插件,信息更详细 ...
- iOS开发常用三方库、插件、知名博客
TimLiu-iOS iOS开发常用三方库.插件.知名博客等等,期待大家和我们一起共同维护,同时也期望大家随时能提出宝贵的意见(直接提交Issues即可). 持续更新... 版本:Objective- ...
- iOS最全的三方库、插件、博客汇总
目录 UI@ 日历三方库@ 下拉刷新@ 模糊效果@ 富文本@ 图表@ 颜色@ 表相关@(TabbleView.Tabbar.即时聊天界面) TableView@ CollectionView@ 隐藏与 ...
- iOS 强大第三方资源库
Github用法 git-recipesGit recipes in Chinese. 高质量的Git中文教程. lark怎样在Github上面贡献代码 my-git有关 git 的学习资料 giti ...
最新文章
- HDU 1257 - 最少拦截系统 ( LIS / 贪心 )
- 机器学习面试必备 CheatSheet
- LoadRunner监控mysql利器-SiteScope(转)
- win7下设置cmd操作mysql的环境变量
- arma找不到合适的模型_新手自学PS找不到合适的素材?5000高清背景素材+150G素材包+滤镜...
- 主要技术指标简介_期货常用技术指标(五)布林线
- 深度学习总结:continuous actions和asyncronous advanteage actor-critic
- 前端内存优化的探索与实践
- 9 WM配置-主数据-定义物料分阶段的范围(Staging Area)
- 仿抖音视频自动播放html,vue 仿抖音视频播放切换
- editthiscookie
- 双系统下Mac可以这样卸载windows系统
- linux nginx 添加插件,linux安装nginx1.9.9实录
- adb interface 驱动问题(保证解决)
- 理解机器翻译模型 Transformer
- wagtail 实现中英文
- u盘文件无法复制是什么原因?有什么解决方法
- MAT分析器中的shallow and retained heap详解
- 部分电商平台为防止爬虫竟然这样做?
- 《云计算与大数据》课程报告
热门文章
- GoEasy小程序即时通讯源码 v1.1.0基于GoEasy提供的websocket通讯服务
- Win7系统忘记登入密码的解决方法
- 使用jQuery和Pure.CSS创建一个可编辑的表格
- Linux: mv, rename单次及批次修改档案名称及后缀(批量修改文件名)
- Magento 默认排序为最新 How to sort Magento products by date added as default
- PHP中使用Filter进行数据安全过滤
- jquery user interface
- 树模型——机器学习面试
- 如何用Pygame写游戏(九)
- oracle数据库解锁表