iOS实现折叠单元格

所属分类: 软件编程 / IOS 阅读数: 321
收藏 0 赞 0 分享

本文实例为大家分享了iOS实现折叠单元格的具体代码,供大家参考,具体内容如下

思路

点击按钮或cell单元格来进行展开收缩, 同时使用一个BOOL值记录单元格展开收缩状态。根据BOOL值对tableView的高度和button的image进行实时变更。

注意点:

在执行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath( 点击当前单元格)方法时,收缩单元格,显示当前点击的单元格的内容。这一步骤的实现是对存储单元格内容的可变数组进行更改。

代码

//ViewController.h 中

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property UITableView *tableView;
@property UIButton *button;   
@property NSMutableArray *imageViewArr; 
@property NSMutableArray *labelArr;  
@property BOOL select;    //记录单元格展开收缩状态

@end
//ViewController.m 中

#import "ViewController.h"
#import "ViewTableViewCell.h"
#import "Masonry.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 
 self.view.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
 
 _imageViewArr = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
 _labelArr = [[NSMutableArray alloc] initWithObjects:@"发起群聊", @"添加朋友", @"扫一扫", @"收付款", @"帮助与反馈", nil];
 
 _tableView = [[UITableView alloc] init];
 [self.view addSubview:_tableView];

 _tableView.frame = CGRectMake(100, 100, 130, 35);
 //以下使用Masonry对tableView进行约束, 约束不是很规范 可忽略
// [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//  make.height.mas_offset(self.view.frame.size.height * 0.0485);
//  make.width.mas_offset(self.view.frame.size.width * 0.335);
//  make.left.equalTo(self.view.mas_left).offset(self.view.frame.size.width * 0.6);
//  make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height * 0.046);
//
// }];
 
 _tableView.delegate = self;
 _tableView.dataSource = self;
 [_tableView registerClass:[ViewTableViewCell class] forCellReuseIdentifier:@"cell"];
 
 _button = [UIButton buttonWithType:UIButtonTypeCustom];
 [self.view addSubview:_button];

 [_button mas_makeConstraints:^(MASConstraintMaker *make) {
  make.left.equalTo(_tableView.mas_right).offset(-28);
  make.top.equalTo(_tableView.mas_top).offset(4);
  make.height.mas_offset(self.view.frame.size.height * 0.0495 * 0.68);
  make.width.mas_offset(self.view.frame.size.width * 0.335 * 0.22);
  
 }];
 [_button setImage:[UIImage imageNamed:@"shou"] forState:UIControlStateNormal];
 [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
 //默认单元格为收缩 select为0
 _select = 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 //根据select的值来判断收缩展开状态,返回相应的行数 
 if(_select == 0) {
  return 1;
 } else {
  return 5;
 }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
 ViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
 cell.iimageView.image = [UIImage imageNamed:_imageViewArr[indexPath.row]];
 cell.label.text = [NSString stringWithString:_labelArr[indexPath.row]];
 return cell;
}

//点击当前单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 //记录当前单元格的imageView 和 Label的内容
 NSString *imageViewStr = [NSString stringWithString:_imageViewArr[indexPath.row]];
 NSString *labelStr = [NSString stringWithString:_labelArr[indexPath.row]];

 //将当前单元格的内容插入可变数组,作为第一个元素
 [_imageViewArr insertObject:imageViewStr atIndex:0];
 [_labelArr insertObject:labelStr atIndex:0];
 
 //同时删除可变数组中当前单元格的原本所在位置
 [_imageViewArr removeObjectAtIndex:indexPath.row + 1];
 [_labelArr removeObjectAtIndex:indexPath.row + 1];
 
 //更新tableView
 [_tableView reloadData];
 
 //调用press方法, 变更tableView的高度 和 button的image
 [self press];
 
}


- (void)press {

 //通过判断select的值, 判断单元格的展开与收缩,更改tableView的高度 和 button的image
 if (_select == 0) {
  _select = 1;
  
  _tableView.frame = CGRectMake(100, 100, 130, 200);
  
  //以下使用masonry对tableView进行更新约束 (以下代码为更新tableView的高度)
//  [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//   make.height.mas_offset(200);
//  }];

  [_button setImage:[UIImage imageNamed:@"kai"] forState:UIControlStateNormal];
  
 } else {
  _select = 0;
  _tableView.frame = CGRectMake(100, 100, 130, 35);
//  [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//   make.height.mas_offset(self.view.frame.size.height * 0.0485);
//  }];

  [_button setImage:[UIImage imageNamed:@"shou"] forState:UIControlStateNormal];
 }
 [_tableView reloadData];
}

@end
// ViewTableViewCell.h 中

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ViewTableViewCell : UITableViewCell

@property UIImageView *iimageView;
@property UILabel *label;

@end
//ViewTableViewCell.m中

#import "ViewTableViewCell.h"

@implementation ViewTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 
 _iimageView = [[UIImageView alloc] init];
 [self.contentView addSubview:_iimageView];
 
 _label = [[UILabel alloc] init];
 [self.contentView addSubview:_label];
 return self;
}

- (void)layoutSubviews {
 [super layoutSubviews];
 _iimageView.frame = CGRectMake(5, 5, 25, 25);
 _label.frame = CGRectMake(37, 5, 80, 25);
 _label.font = [UIFont systemFontOfSize:15];
}

@end

效果图如下

初始状态

点击cell或点击按钮,显示如下:

点击任意cell, 例如点击扫一扫,单元格收回,如图

再次展开单元格, cell的内容如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多