动态展开tableView的cell[1]

源码地址:https://github.com/xerxes235/HVTableView

虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-!!,不信,你可以去下载他的demo试一下:)

本人运行时的效果如下图:

源码:

  1. RootViewController.m
  1. //
  2. // RootViewController.m
  3. // AnimationTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "RootViewController.h"
  9. #import "HVTableView.h"
  10. #import "YXCell.h"
  11.  
  12. @interface RootViewController ()<HVTableViewDelegate, HVTableViewDataSource>
  13.  
  14. @property (nonatomic, strong) HVTableView *showTable;
  15. @property (nonatomic, strong) NSArray *picAry;
  16.  
  17. @end
  18.  
  19. @implementation RootViewController
  20.  
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24.  
  25. // 图片源
  26. _picAry = @[[UIImage imageNamed:@"1.jpg"],
  27. [UIImage imageNamed:@"2.jpg"],
  28. [UIImage imageNamed:@"3.jpg"],
  29. [UIImage imageNamed:@"4.jpg"],
  30. [UIImage imageNamed:@"5.jpg"],
  31. [UIImage imageNamed:@"6.jpg"],
  32. [UIImage imageNamed:@"7.jpg"],
  33. [UIImage imageNamed:@"8.jpg"],
  34. [UIImage imageNamed:@"9.jpg"],
  35. [UIImage imageNamed:@"10.jpg"]];
  36.  
  37. // tableView
  38. _showTable = \
  39. [[HVTableView alloc] initWithFrame:self.view.bounds
  40. expandOnlyOneCell:YES
  41. enableAutoScroll:YES];
  42. _showTable.HVTableViewDelegate = self;
  43. _showTable.HVTableViewDataSource = self;
  44. [_showTable reloadData];
  45.  
  46. // 加载进视图
  47. [self.view addSubview:_showTable];
  48. }
  49.  
  50. #pragma mark - 各个代理
  51.  
  52. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  53. {
  54. return ;
  55. }
  56.  
  57. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  58. {
  59. return _picAry.count;
  60. }
  61.  
  62. //==============================================
  63. #pragma mark 展开时的cell(可以添加动画)
  64. //==============================================
  65. -(void)tableView:(UITableView *)tableView
  66. expandCell:(UITableViewCell *)cell
  67. withIndexPath:(NSIndexPath *)indexPath
  68. {
  69. YXCell *tmpCell = (YXCell *)cell;
  70. [UIView animateWithDuration:0.3f animations:^{
  71. tmpCell.showImageView.frame = CGRectMake(, , , );
  72. }];
  73.  
  74. [UIView animateWithDuration:0.5f animations:^{
  75. tmpCell.name.frame = CGRectMake(, , , );
  76. }];
  77. }
  78.  
  79. //==============================================
  80. #pragma mark 收缩时的cell(可以添加动画)
  81. //==============================================
  82. -(void)tableView:(UITableView *)tableView
  83. collapseCell:(UITableViewCell *)cell
  84. withIndexPath:(NSIndexPath *)indexPath
  85. {
  86. YXCell *tmpCell = (YXCell *)cell;
  87. [UIView animateWithDuration:0.3f animations:^{
  88. tmpCell.showImageView.frame = CGRectMake(, , , );
  89. tmpCell.name.frame = CGRectMake(, , , );
  90. }];
  91. }
  92.  
  93. //==============================================
  94. #pragma mark 返回高度
  95. //==============================================
  96. -(CGFloat)tableView:(UITableView *)tableView
  97. heightForRowAtIndexPath:(NSIndexPath *)indexPath
  98. isExpanded:(BOOL)isexpanded
  99. {
  100. if (isexpanded == YES)
  101. {
  102. // 展开时的高度
  103. return ;
  104. }
  105. else
  106. {
  107. // 没有展开时的高度
  108. return ;
  109. }
  110. }
  111.  
  112. //==============================================
  113. #pragma mark 返回高度
  114. //==============================================
  115. -(UITableViewCell *)tableView:(UITableView *)tableView
  116. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  117. isExpanded:(BOOL)isExpanded
  118. {
  119. static NSString *CellIdentifier = @"aCell";
  120. YXCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  121. if (cell == nil)
  122. {
  123. cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  124. reuseIdentifier:CellIdentifier];
  125. }
  126.  
  127. // 选择时没有颜色
  128. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  129.  
  130. // 加载图片
  131. cell.showImageView.image = _picAry[indexPath.row];
  132.  
  133. // 没有展开cell,进行一些设置(不要添加任何动画)
  134. if (isExpanded == NO)
  135. {
  136. cell.showImageView.frame = CGRectMake(, , , );
  137. cell.name.frame = CGRectMake(, , , );
  138. }
  139. // 展开的cell,进行一些设置(不要添加任何动画)
  140. else
  141. {
  142. cell.showImageView.frame = CGRectMake(, , , );
  143. cell.name.frame = CGRectMake(, , , );
  144. }
  145.  
  146. return cell;
  147. }
  148.  
  149. @end
  1. YXCell.h
  1. //
  2. // YXCell.h
  3. // AnimationTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import <UIKit/UIKit.h>
  9.  
  10. @interface YXCell : UITableViewCell
  11.  
  12. @property (nonatomic, strong) UILabel *name;
  13. @property (nonatomic, strong) UIImageView *showImageView;
  14.  
  15. @end
  1. YXCell.m
  1. //
  2. // YXCell.m
  3. // AnimationTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "YXCell.h"
  9.  
  10. @implementation YXCell
  11.  
  12. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  13. {
  14. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  15. if (self)
  16. {
  17. // 尺寸在外面的cell设定
  18. _showImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
  19. [self addSubview:_showImageView];
  20.  
  21. _name = [[UILabel alloc] initWithFrame:CGRectZero];
  22. _name.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:.f];
  23. _name.text = @"YouXianMing";
  24. _name.textColor = [UIColor blackColor];
  25. [self addSubview:_name];
  26.  
  27. self.layer.masksToBounds = YES;
  28. }
  29. return self;
  30. }
  31.  
  32. @end

关键的几步:

怎么实现复杂逼格高的动画?这个就需要你的想象力来脑补了-_-!!,没有实现不出来的效果,只有想不出来的效果:)

附录:

其实笔者深刻理解他的原理,然后尝试着自己写了一个,不过,那恶心的重用问题,不自己亲自动手是不理解别人写代码的用心良苦的-_-!!!!!

先共享源码供君尝试:

  1. //
  2. // YXCell.h
  3. // ExpendTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import <UIKit/UIKit.h>
  9.  
  10. @interface YXCell : UITableViewCell
  11.  
  12. @property (nonatomic, strong) UIView *showView;
  13.  
  14. @end
  1. //
  2. // YXCell.m
  3. // ExpendTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "YXCell.h"
  9.  
  10. @implementation YXCell
  11.  
  12. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  13. {
  14. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  15. if (self)
  16. {
  17. _showView = [[UIView alloc] initWithFrame:CGRectZero];
  18. _showView.backgroundColor = [UIColor redColor];
  19. [self addSubview:_showView];
  20. }
  21. return self;
  22. }
  23.  
  24. @end
  1. //
  2. // RootViewController.m
  3. // ExpendTableView
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "RootViewController.h"
  9. #import "YXCell.h"
  10.  
  11. @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
  12.  
  13. {
  14.  
  15. BOOL flag[];
  16.  
  17. }
  18.  
  19. @property (nonatomic, strong) UITableView *tableView;
  20.  
  21. @end
  22.  
  23. @implementation RootViewController
  24.  
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28.  
  29. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
  30. style:UITableViewStylePlain];
  31. _tableView.delegate = self;
  32. _tableView.dataSource = self;
  33.  
  34. [self.view addSubview:_tableView];
  35. }
  36.  
  37. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  38. {
  39. return ;
  40. }
  41.  
  42. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  43. {
  44. return ;
  45. }
  46.  
  47. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  48. {
  49. if (flag[indexPath.row] == YES)
  50. {
  51. return .f;
  52. }
  53. else
  54. {
  55. return .f;
  56. }
  57. }
  58.  
  59. //==============================================
  60. #pragma mark 根据cell状态进行相关设置
  61. //==============================================
  62. -(UITableViewCell *)tableView:(UITableView *)tableView
  63. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  64. {
  65. static NSString *CellIdentifier = @"aCell";
  66. YXCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  67. if (cell == nil)
  68. {
  69. cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  70. reuseIdentifier:CellIdentifier];
  71. }
  72.  
  73. // 选择时没有颜色
  74. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  75.  
  76. if (flag[indexPath.row] == YES)
  77. {
  78. [UIView animateWithDuration:.f animations:^{
  79. cell.showView.frame = CGRectMake(, , , );
  80. }];
  81. }
  82. else
  83. {
  84. cell.showView.frame = CGRectMake(-, , , );
  85. }
  86.  
  87. return cell;
  88. }
  89.  
  90. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92. if (flag[indexPath.row] == NO)
  93. {
  94. for (int i = ; i < ; i++)
  95. {
  96. flag[i] = NO;
  97. }
  98.  
  99. flag[indexPath.row] = YES;
  100.  
  101. [tableView beginUpdates];
  102. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  103. withRowAnimation:UITableViewRowAnimationAutomatic];
  104. [tableView endUpdates];
  105. }
  106. else
  107. {
  108. flag[indexPath.row] = NO;
  109.  
  110. [tableView beginUpdates];
  111. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  112. withRowAnimation:UITableViewRowAnimationAutomatic];
  113. [tableView endUpdates];
  114. }
  115. }
  116.  
  117. @end

以下三个地方是相互配合的,但还是难以解决重用问题-_-!!!!

动态展开tableView的cell[1]的更多相关文章

  1. 动态展开tableView的cell[2]

    动态展开tableView的cell[2] http://code4app.com/ios/%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0cell/53845f8a933bf ...

  2. 使用HVTableView动态展开tableView中的cell

    使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...

  3. IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度

    一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...

  4. 动态切换tableView中的cell的种类

    动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...

  5. 解决tableView中cell动态加载控件的重用问题

    解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...

  6. jqGrid subGrid配置 如何首次加载动态展开所有的子表格

    有时候需求需要默认加载表格的时候把子表格的数据也显示出来,经过研究相关SubGrids API配置如下: 属性 类型 描述 默认值 subGrid boolean 设置为true启用子表格.如果启用子 ...

  7. 关于tableview内cell自定义的注册以及创建

    自定义cell的方法主要有两种,storyboard以及xib(假设新建的是cellTableViewCell类) 比较倾向于xib方式使用xib在xib文件内将自定义的cell绘制好后导入到调用文件 ...

  8. TableView的cell加载倒计时重用问题解决方案

    TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...

  9. IOS 关于tableview中cell的长按手势

    说明:虽然是tableview中cell的长按手势  但是手势是添加在tableview上的 UILongPressGestureRecognizer *longpress = [[UILongPre ...

随机推荐

  1. Ubuntu14.04下Ambari安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)

    不多说,直接上干货! 写在前面的话 (1) 最近一段时间,因担任我团队实验室的大数据环境集群真实物理机器工作,至此,本人秉持负责.认真和细心的态度,先分别在虚拟机上模拟搭建ambari(基于CentO ...

  2. Android Studio 和 gradle 修改缓存文件夹路径

    Android Studio的缓存文件主要有四个文件夹,分别是 .android 这个文件夹是Android SDK生成的AVD(Android Virtual Device Manager)即模拟器 ...

  3. Hudson-ci/Using Hudson/Installing Hudson/Installing Hudson RPM--官方文档

    < Hudson-ci‎ | Using Hudson‎ | Installing Hudson(Redirected from Hudson-ci/Installing Hudson RPM) ...

  4. Node.js的基础知识(一)

    一.Buffer类 1.创建缓冲区的三种方式 var buffer = new Buffer(10); console.log(buffer); var buffer2 = new Buffer([1 ...

  5. Mysql的with rollup分组统计功能(5.1以上版本)

    RollUp是上卷功能,类似于数据挖掘中的上卷操作. ROLLUp的功能和Order by功能是互斥的. mysql> SELECT year, SUM(profit) FROM sales G ...

  6. 【译】Steve Yegge的文章《Practicing Programming》

    与你所相信的恰恰相反,单纯地每天埋头于工作并不能算是真正意义上的锻炼——参加会议并不能锻炼你的人际交往能力:回复邮件并不能提高你的打字水平.你必须定期留出时间,集中锻炼,这样才能把事情做得更好.我认识 ...

  7. unity 和 iOS/Android 信息交互(方法调用)

    参考文章均来源于[大神雨松momo]的文章. unity -> iOS // unity 程序 usingSystem.Runtime.InteropServices; usingUnityEn ...

  8. 如鹏网学习笔记(八)CSS

    CSS 一.CSS简介 1,CSS (Cascading Style Sheets) 级联样式表 ,是一种计算机语言,用来控制HTML内容的显示效果 2,CSS预先定义了众多的和显示效果有关的样式属性 ...

  9. HDU 1575 Tr A----矩阵相乘题。

    Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. MYSQL建表问题(转)

    今天在dos下准备新建一个数据表,但一直出错,如下 后面在网上查了好久,终于找到了原因.创建 MySql 的表时,表名和字段名外面的符号 ` 不是单引号,而是英文输入法状态下的反单引号,也就是键盘左上 ...