UITableVIew是iOS开发中使用最为广泛的一种控件,对于UITableView的基本用法本文不做探讨,本文主要是针对UITableView的展开与收缩进行阐述,在文章的后面也会探讨一下横向table的用法:

1. UITableView的展开与收缩

下面通过两幅图来了解UITableView的展开与收缩的效果:

这种展开与收缩的原理其实非常简单,在使用该TableVIew的时候需要准备两个自定义的Cell,一个是未展开情况下的Cell,一个是展开后
的Cell,当点击某一行的展开/收缩按钮时,将该行替换为展开/收缩后的Cell,并将其他所有行全部收缩,原理很简单,下面上代码:

(1)扩展UITableView的部分方法:

创建一个UITableVIew的子类,名称为:ExtensibleTableView

.h文件中:

@protocol ExtensibleTableViewDelegate <NSObject>

@required

//返回展开之后的cell

- (UITableViewCell *)tableView:(UITableView *)tableView extendedCellForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回展开之后的cell的高度

- (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

@end

@interface ExtensibleTableView :
UITableView

{

//当前被展开的索引

NSIndexPath *currentIndexPath;

//  id<ExtensibleTableViewDelegate> delegate_extend;

}

@property(nonatomic,retain)id delegate_extend;

@property(nonatomic,retain)NSIndexPath *currentIndexPath;

//将indexPath对应的row展开

- (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop;

//将展开的cell收起

- (void)shrinkCellWithAnimated:(BOOL)animated;

//查看传来的索引和当前被选中索引是否相同

- (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath;

@end

在.m文件中

@implementation ExtensibleTableView

@synthesize delegate_extend;

@synthesize currentIndexPath;

- (id)init

{

self.currentIndexPath =nil;

return [superinit];

}

//重写设置代理的方法,使为UITableView设置代理时,将子类的delegate_extend同样设置

- (void)setDelegate:(id<UITableViewDelegate>)delegate

{

self.delegate_extend = delegate;

[super setDelegate:delegate];

}

/*

将indexPath对应的row展开

params:

animated:是否要动画效果

goToTop:展开后是否让到被展开的cell滚动到顶部

*/

- (void)extendCellAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated goToTop:(BOOL)goToTop

{

//被取消选中的行的索引

NSIndexPath *unselectedIndex = [NSIndexPathindexPathForRow:[self.currentIndexPathrow]inSection:[self.currentIndexPathsection]];

//要刷新的index的集合

NSMutableArray *array1 = [[NSMutableArrayalloc]init];

//若当前index不为空

if(self.currentIndexPath)

{

//被取消选中的行的索引

[array1 addObject:unselectedIndex];

}

//若当前选中的行和入参的选中行不相同,说明用户点击的不是已经展开的cell

if(![selfisEqualToSelectedIndexPath:indexPath])

{

//被选中的行的索引

[array1 addObject:indexPath];

}

//将当前被选中的索引重新赋值

self.currentIndexPath = indexPath;

if(animated)

{

[selfreloadRowsAtIndexPaths:array1withRowAnimation:UITableViewRowAnimationFade];

}

else

{

[selfreloadRowsAtIndexPaths:array1withRowAnimation:UITableViewRowAnimationNone];

}

if(goToTop)

{

//tableview滚动到新选中的行的高度

[selfscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionTopanimated:YES];

}

[array1 release];

}

//将展开的cell收起

- (void)shrinkCellWithAnimated:(BOOL)animated

{

//要刷新的index的集合

NSMutableArray *array1 = [[NSMutableArrayalloc]init];

if(self.currentIndexPath)

{

//当前展开的cell的索引

[array1 addObject:self.currentIndexPath];

//将当前展开的cell的索引设为空

self.currentIndexPath =nil;

[selfreloadRowsAtIndexPaths:array1withRowAnimation:UITableViewRowAnimationFade];

}

[array1 release];

}

//查看传来的索引和当前被选中索引是否相同

- (BOOL)isEqualToSelectedIndexPath:(NSIndexPath *)indexPath

{

if(self.currentIndexPath)

{

return ([self.currentIndexPathrow] == [indexPathrow]) && ([self.currentIndexPathsection]
== [indexPathsection]);

}

return
NO;

}

/*

重写了这个方法,却无效,因为这个方法总在didSelect之前调用,很奇怪。因为无法重写该方法,所以ExtensibleTableView不算完善,因为还有额外的代码需要在heightForRowAtIndexPath和cellForRowAtIndexPath中。哪个找到完善的方法后希望可以与qq82934162联系或者在http://borissun.iteye.com来留言

*/

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if([self.currentIndexPathrow] == [indexPathrow])

{

return [self.delegate_extendtableView:selfextendedHeightForRowAtIndexPath:indexPath];

}

return [superrowHeight];

}

@end

这个类时在UITableView的基础上扩展了两个方法,一个是展开情况下的CellForRow方法,一个是在展开情况下的返回heightForRow方法

(2) 准备两个自定义的Cell

如下图:

          

可以看出这两个Cell上半部分完全一样,主要差别是展开后的Cell多了下面的一部分

(3) 在ViewController中实现该TableVIew的部分方法:

在ViewController文件中:

ExtensibleTableView *ccTableView=[[ExtensibleTableViewalloc]initWithFrame:CGRectMake(0,163,ModelSize.width,ModelSize.height-20-44-49-163-40)];

ccTableView.delegate=self;

ccTableView.dataSource=self;

ccTableView.delegate_extend=self;     //这个代理就是展开行的代理

下面实现UITableVIew的方法:

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

{

return
[dataArr count];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

CGFloat hei=0.0f;

if([ccTableViewisEqualToSelectedIndexPath:indexPath])

{

return [selftableView:ccTableViewextendedHeightForRowAtIndexPath:indexPath];//展开后的行高

}else

{

hei= 50;   // 未展开下的行高

}

return hei;

}

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

{

//若当前行被选中,则返回展开的cell

if([ccTableViewisEqualToSelectedIndexPath:indexPath])

{

return [selftableView:ccTableViewextendedCellForRowAtIndexPath:indexPath];

}

static NSString *CellIdentifier =@"CCCell";

UINib *nib = [UINibnibWithNibName:@"CCCell"bundle:nil];

[tableView registerNib:nib
forCellReuseIdentifier:CellIdentifier];

CCCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell ==
nil)

{

cell = [[[CCCell
alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier]autorelease];

}

cell.selectionStyle=UITableViewCellSelectionStyleNone;

//在此设置Cell的相关数据

UIButton *btn=[[UIButtonalloc]initWithFrame:CGRectMake(ModelSize.width-45,3,45,
45)];

[btn setImage:[UIImageselfimageNamed:@"tragile_down@2x.png"]forState:UIControlStateNormal];

btn.tag=indexPath.row;

[btn addTarget:selfaction:@selector(operateCell:)forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:btn];

[btn release];

return cell;

}

//返回展开之后的cell

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

{

//每个cell的标识

NSString *CellTableIdentifier=@"CCopenCell";

//获得cell

UINib *nib = [UINibnibWithNibName:@"CCopenCell"bundle:nil];

[tableView registerNib:nib
forCellReuseIdentifier:CellTableIdentifier];

CCopenCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellTableIdentifier];

if (cell == nil)

{

cell = [[[CCopenCell
alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellTableIdentifier]autorelease];

cell.frame=CGRectMake(0,0,ModelSize.width
,0);

}

cell.selectionStyle=UITableViewCellSelectionStyleNone;

// 在此设置cell的相关数据

UIButton *btn=[[UIButtonalloc]initWithFrame:CGRectMake(ModelSize.width-45,3,45,45)];

[btn setImage:[UIImageselfimageNamed:@"tragile_down@2x.png"]forState:UIControlStateNormal];

btn.tag=indexPath.row;

[btn addTarget:selfaction:@selector(operateCell:)forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:btn];

[btn release];

return cell;

}

//返回展开之后的cell的高度

- (CGFloat)tableView:(UITableView *)tableView extendedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 95;

}

OK,以上就是展开与收缩TableVIew的代码,本人已经多次使用,确认无误,如果疑问请联系

最后我们看看横向TableVIew的实现方法:

_tableView.transform
= CGAffineTransformMakeRotation(-M_PI / 2);

cell.contentView.transform
= CGAffineTransformMakeRotation(M_PI / 2); 
方法二:横向UITableView已经有开源实现了  ,EasyTableView,https://github.com/alekseyn/EasyTableView

ios知识点总结——UITableView的展开与收缩及横向Table的更多相关文章

  1. 【iOS系列】-UITableViewCell的展开与收缩的实现思路

    UITableViewCell的展开与收缩的实现思路 现在项目中很多地方都会用到,所以我这里介绍一种可以复用的思路,这与文章后面的Swift的实现思路不同,具体大家可自行对比. Demo项目地址 开始 ...

  2. iOS开发系列--UITableView全面解析

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  3. 在iOS中怎样创建可展开的Table View?(下)

    接上篇:在iOS中怎样创建可展开的Table View?(上) 展开和合拢 我猜这部分可能是你最期望的了,因为本次教程的目标将会在在部分实现.第一次我们设法让顶层的cell,在它们点击的时候展开或者合 ...

  4. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  5. ios-tableViewcell展开与收缩动画处理

    [前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...

  6. DataGridView之行的展开与收缩

    很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据. 比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室. 我们来说一下在Dat ...

  7. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  8. dhtmlxtree 节点 展开收缩:新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开、收缩状态)

    dhtmlxtree 节点 展开收缩通常情况我们按 +- 就实现了 展开收缩 功能,为了方便我们新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开.收缩状态) tree = new dh ...

  9. js 实现内容的展开和收缩

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

随机推荐

  1. Asp.net IIS Express 无法启动 解决办法

    http://www.mamicode.com/info-detail-1893424.html 一 .其他项目都可以,就这么一个不行 用记事本或者其他什么文本编辑器,打开项目的.csproj文件,定 ...

  2. js解析xml浏览器兼容性处理

    /****************************************************************************** 说明:xml解析类 ********** ...

  3. SQL SERVER 日期转换大全

    博客转自:http://blog.csdn.net/baiduandxunlei/article/details/9180075 CONVERT(data_type,expression[,style ...

  4. openldap 搭建

    环境构建 1)软件安装: yum -y install openldap-servers openldap-clients openldap openldap-devel migrationtools ...

  5. SourceTree管理工具的一些使用总结

    一.冲突解决 在团队合作中,如果两个人同时修改一个文件 ,这个时候如果合并他人提交的代码是会产生冲突的,怎么解决? 1.先将代码提交至本地服务器 2.合并他人代码,这个时候在工作副本中会显示我们冲突的 ...

  6. Python爬虫——城市公交、地铁站点和线路数据采集

    本篇博文为博主原创,转载请注明. 城市公交.地铁数据反映了城市的公共交通,研究该数据可以挖掘城市的交通结构.路网规划.公交选址等.但是,这类数据往往掌握在特定部门中,很难获取.互联网地图上有大量的信息 ...

  7. C语言的学习

    一.文件的使用方式 r  只读  rb只读  r+ rb+(不带b的为已存在的文本文件,带b的为二进制文件(binary),带+号的为读写文件) w 只写 wb只写 a 追加  ab追加 二.说明 1 ...

  8. Java学习笔记9(面向对象二:this、继承、抽象类)

    就近原则: 类中的方法中的变量和成员变量重名时,调用类的方法时候,生效的是方法中的变量,如果方法中没有定义变量,才会去成员变量中寻找 于是,提出了this关键字,为了区分重名问题 public cla ...

  9. WinForm响应式布局设计实践

    引言 创建响应式WinForm应用程序并不那么简单. 响应式布局,在此我指的是应用程序在不同屏幕分辨率下的可用性. 对于WinForm应用程序,我们需要明确地根据分辨率来调整控件的大小和重新定位. 虽 ...

  10. python中将字典形式的数据循环插入Excel

    1.我们看到字典形式的数据如下所示 list=[["2891-1", "D"],["2892-1", "D"],[&qu ...