实现效果如图:

查看更多功能在很多app种都有应用,在这里简单的实现,介绍实现流程:

一个tableViewCell中包含一个collectionView,"查看更多"按钮是tableView的footerView

在控制器中ViewController .m中

  1. #import "ViewController.h"
  2. #import "ZSTableViewCell.h"
  3.  
  4. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
  5.  
  6. @property (nonatomic,strong) UITableView *tableView;
  7.  
  8. //存放标题的数组
  9. @property (nonatomic,strong) NSArray *titleArray;
  10.  
  11. @property (nonatomic,strong) UIButton *changeButton;
  12.  
  13. @property (nonatomic,assign) BOOL isOpen;
  14.  
  15. @property (nonatomic,assign) NSInteger showButtonNumber;
  16.  
  17. @end
  18.  
  19. @implementation ViewController
  20.  
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23.  
  24. self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  25.  
  26. [self.view addSubview:self.tableView];
  27.  
  28. self.tableView.delegate = self;
  29. self.tableView.dataSource = self;
  30.  
  31. [self.tableView registerClass:[ZSTableViewCell class] forCellReuseIdentifier:@"TheCell"];
  32.  
  33. self.title = @"查看更多/收起";
  34. self.isOpen = NO;
  35. [self.changeButton setTitle:@"查看更多" forState:UIControlStateNormal];
  36. self.changeButton.backgroundColor = [UIColor clearColor];
  37.  
  38. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  39. flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - )/, );
  40. _showButtonNumber = ;
  41. _titleArray = @[@"东方不败",@"岳不群",@"林平之",@"令狐冲",@"岳灵珊",@"任我行",@"左冷禅",@"蓝凤凰",@"向问天",@"田伯光",@"风清扬",@"任盈盈",@"路人甲",@"路人乙",@"路人丙"];
  42. }
  43. #pragma mark --懒加载
  44. //查看更多/收起按钮
  45. - (UIButton *)changeButton{
  46. if (_changeButton == nil) {
  47. _changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  48. _changeButton.frame = CGRectMake(, , [UIScreen mainScreen].bounds.size.width, );
  49. [_changeButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  50. [_changeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  51. [_changeButton setBackgroundColor:[UIColor whiteColor]];
  52. _changeButton.layer.cornerRadius = ;
  53. _changeButton.layer.masksToBounds = YES;
  54. _changeButton.layer.borderWidth = ;
  55. _changeButton.layer.borderColor = [UIColor greenColor].CGColor;
  56.  
  57. }
  58. return _changeButton;
  59. }
  60. //button点击事件
  61. - (void)buttonClick:(UIButton *)sender{
  62. //如果不是展开状态
  63. if (self.isOpen == NO) {
  64. [self.changeButton setTitle:@"收起" forState:UIControlStateNormal];
  65. self.isOpen = YES;
  66. _showButtonNumber = _titleArray.count;
  67.  
  68. }else{
  69. [self.changeButton setTitle:@"查看更多" forState:UIControlStateNormal];
  70. self.isOpen = NO;
  71. _showButtonNumber = ;
  72. }
  73. //刷新 动画效果 第0个section NSIndexSet索引集合
  74. [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:] withRowAnimation:(UITableViewRowAnimationAutomatic)];
  75. /*
  76.  
  77. UITableViewRowAnimationFade, 消失
  78. UITableViewRowAnimationRight, 从右滑行 // slide in from right (or out to right)
  79. UITableViewRowAnimationLeft,
  80. UITableViewRowAnimationTop,
  81. UITableViewRowAnimationBottom,
  82. UITableViewRowAnimationNone, // available in iOS 3.0
  83. UITableViewRowAnimationMiddle,
  84. UITableViewRowAnimationAutomatic 自动
  85. */
  86.  
  87. }
  88.  
  89. #pragma mark --
  90. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  91. return ;
  92. }
  93.  
  94. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  95. return ;
  96. }
  97.  
  98. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  99. //根据标识返回不同的高度
  100. if (self.isOpen == YES) {
  101. //因为每行有4个item,要多出空余的item
  102. CGFloat height = (self.titleArray.count / + ) * ;
  103. return height;
  104. }else{
  105. return ;
  106. }
  107. }
  108.  
  109. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  110. return 0.5;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  113. return ;
  114. }
  115.  
  116. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  117. ZSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TheCell" forIndexPath:indexPath];
  118. [cell setupCellWithNum:_showButtonNumber ButtonNameArr:_titleArray];
  119.  
  120. cell.buttonClick = ^(NSInteger index){
  121. NSLog(@"点击的按钮标签为%ld",index);
  122. };
  123.  
  124. return cell;
  125. }
  126. //footview
  127. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  128. UIView *firstFootView = [[UIView alloc]initWithFrame:self.changeButton.frame];
  129. [firstFootView addSubview:self.changeButton];
  130. firstFootView.backgroundColor = [UIColor orangeColor];
  131. return firstFootView;
  132. }
  133.  
  134. - (void)didReceiveMemoryWarning {
  135. [super didReceiveMemoryWarning];
  136. // Dispose of any resources that can be recreated.
  137. }
  138.  
  139. @end

在tableViewCell的.h中

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ZSTableViewCell : UITableViewCell
  4. //点击cell的回调
  5. @property (nonatomic,copy) void (^buttonClick)(NSInteger index);
  6.  
  7. - (void)setupCellWithNum:(NSInteger)buttonCount ButtonNameArr:(NSArray *)buttonArray;
  8.  
  9. @end

tableViewCell的.m

  1. #import "ZSTableViewCell.h"
  2. #import "TheItemCell.h"
  3.  
  4. @interface ZSTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
  5.  
  6. @property (nonatomic,strong) UICollectionView *collectionView;
  7.  
  8. @property (nonatomic,assign) NSInteger cellNum;//接受控制器传来的数组个数
  9.  
  10. @property (nonatomic,strong) NSArray *buttonTitleArray;
  11.  
  12. @end
  13. @implementation ZSTableViewCell
  14.  
  15. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  16.  
  17. self = [super initWithStyle: style reuseIdentifier:reuseIdentifier];
  18. if (self) {
  19.  
  20. //流水布局
  21. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  22. flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - )/, );
  23. //行间距
  24. flowLayout.minimumLineSpacing = ;
  25. //列间距
  26. flowLayout.minimumInteritemSpacing = ;
  27. //设置item偏移量 上 左 下 右
  28. flowLayout.sectionInset = UIEdgeInsetsMake(, , , );
  29. self.collectionView.scrollEnabled = NO;
  30.  
  31. CGFloat height = ( / +) * ;
  32. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width,height) collectionViewLayout:flowLayout];
  33. [self.contentView addSubview:self.collectionView];
  34. //注册,必须先创建完collectionView,并且添加到父控件,才能注册,不然会报错
  35. [self.collectionView registerClass:[TheItemCell class] forCellWithReuseIdentifier:@"Cell"];
  36. self.collectionView.backgroundColor = [UIColor whiteColor];
  37. self.backgroundColor = [UIColor orangeColor];
  38. }
  39.  
  40. self.collectionView.delegate = self;
  41. self.collectionView.dataSource = self;
  42. return self;
  43. }
  44.  
  45. - (void)setupCellWithNum:(NSInteger)buttonCount ButtonNameArr:(NSArray *)buttonArray{
  46. self.cellNum = buttonCount;
  47. self.buttonTitleArray = buttonArray;
  48. [self.collectionView reloadData];
  49. }
  50. #pragma mark --
  51. //返回item
  52. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  53. NSLog(@"%ld",(long)_cellNum);
  54. return _cellNum;
  55. }
  56. //返回组
  57. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  58. return ;
  59. }
  60.  
  61. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  62. TheItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  63. cell.label.text = self.buttonTitleArray[indexPath.item];
  64. cell.backgroundColor = [UIColor blueColor];
  65. return cell;
  66. }
  67. //点击cell
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  69. self.buttonClick(indexPath.row);
  70. }
  71.  
  72. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  73. [super setSelected:selected animated:animated];
  74.  
  75. // Configure the view for the selected state
  76. }
  77.  
  78. @end

在collectionViewCell.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface TheItemCell : UICollectionViewCell
  4.  
  5. @property (nonatomic,strong) UILabel *label;
  6.  
  7. @end

collectionViewCell的.m中

  1. #import "TheItemCell.h"
  2.  
  3. @implementation TheItemCell
  4.  
  5. - (instancetype)initWithFrame:(CGRect)frame{
  6. self = [super initWithFrame:frame];
  7. if (self) {
  8. self.label = [[UILabel alloc]initWithFrame:self.bounds];
  9. self.label.textAlignment = NSTextAlignmentCenter;
  10. [self.contentView addSubview:self.label];
  11. }
  12. return self;
  13. }
  14.  
  15. @end

iOS中"查看更多/收起"功能实现的更多相关文章

  1. iOS Masonry 查看更多 收起

    Masonry 查看更多 收起效果实现,带动画 demo下载地址: https://github.com/qqcc1388/MasonryDemo

  2. 如何在ios中集成微信登录功能

    在ios中集成微信的登录功能有两种方法 1 用微信原生的api来做,这样做的好处就是轻量级,程序负重小,在Build Settings 中这样设置 然后设置 友盟的设置同上,但是要注意,加入你需要的所 ...

  3. ovs加dpdk在日志中查看更多运行细节的方法

    想查看更多dpdk+ovs的更多运行细节,可以采用以下方法,增加更多运行日志. 在终端输入: ovs-appctl vlog/set dpdk:file:dbg ovs-appctl vlog/set ...

  4. iOS中键盘的收起

    在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法之外,还有另外三种方法: 重载UIViewController中的touchesBegin方 ...

  5. win10 关闭 “在时间线中查看更多日期” 提示

    在组策略中,禁用允许上传用户活动

  6. iOS中打电话、打开网址、发邮件、发短信等

    常用小功能 小功能简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等 打电话-方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [ ...

  7. iOS 用UISearchDisplayController实现查找功能

    UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用 示例代码如下: // // WKRootViewController ...

  8. JS点击查看更多内容 控制段落文字展开折叠

    JavaScript+jQuery实现的文字展开折叠效果,点击文字后文字内容会完整的显示出来,控制段落来显示文字,不需要的时候,可以再次点击后将内容折叠起来,也就是隐藏了一部分内容.点击查看更多的功能 ...

  9. 微信小程序——收起和查看更多功能

    项目中做一些列表的时候,可能会需要做到 查看更多 及 收起功能,如下图所示: 大概的需求就是默认只显示2条,点击[查看更多]显示全部,点击[收起]还原. 实现的方法千万种.我来讲一下我的实现思路: 1 ...

随机推荐

  1. web项目从域名申请到发布

    http://wenku.baidu.com/link?url=H-Hu2nvzFRirdxO3xzrWCWfc4WJFLyFjsxak5MFwOuzQfgTawJLXC4vAc4xYAIySxn59 ...

  2. linux学习的哲学层面的思考-架构

    参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...

  3. java应用测试报告生成(二):利用ant的build.xml生成测试报告

    1.将写好的项目导出 在工程下会生成一个build.xml的蚂蚁图标的文件. 2.右击该文件,选择run as Ant build 其中的测试目录是可以选择的,如果涉及到顺序也可以调整顺序 3.执行后 ...

  4. Timewarp 一种生成当中帧技术,异步时间扭曲(Asynchronous Timewarp)

    翻译: https://www.oculus.com/blog/asynchronous-timewarp/    异步时间扭曲(Asynchronous Timewarp 时间扭曲,即调整时长) 关 ...

  5. 创建zend framework 项目要注意的

    1.必须要设置变量环境 我的电脑右击-属性-高级-环境变量 则在环境变量中添加 变量名:PATH 环境值:D:\phpserver\php5.4;D:\ZendFramework\bin 把php.e ...

  6. codeforces--376D--376F(前缀和优化)

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. JS中获取页面单选框radio和复选框checkbox中当前选中的值

    单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...

  8. 使用JavaCV/OpenCV抓取并存储摄像头图像

    http://blog.csdn.net/ljsspace/article/details/6702178  分类: 图形图像(3)  版权声明:本文为博主原创文章,未经博主允许不得转载. 本程序通过 ...

  9. jQuery validation

    之前做客户端验证感觉自己javascript 不行,虽然能写出来一完整的验证,但从不自信,一直觉得客户端验证是比较繁琐的事情,但是又不能不做,只到最开始接触ajax ,遇到了一个jQuery vali ...

  10. 【转】PHP里的basename函数不支持中文名的解决

    今天用到basename 函数获取文件名称时,发现如果是中文的文件名返回只有后缀的空文件名(如:.pdf)    string basename ( string path [, string suf ...