iOS开发之tableView cell的展开收回功能实现代码

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

一、实现方法

例如好友分组,分为好友和陌生人两组,实现点击好友和陌生人展开或收回该分组对应的cell的功能。

实现:可以分组对应tableView的section,点击section展开和收回cell。

创建一个临时数组selectedArr存储需要展开的section。点击section是判断selectedArr是否包含该组,如果包含则移除,不包含则添加到selectedArr。

展示selectedArr包含组的cell。

二、代码实现

#import "ZCellGroupController.h"

@interface ZCellGroupController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;//分组
@property (nonatomic, strong) NSArray *friendsArray;//每组对应内容
@property (nonatomic, strong) NSMutableArray *selectedArr;//存储需要展开的cell组

@end

@implementation ZCellGroupController


-(void)viewDidLoad {
  [super viewDidLoad];
  self.selectedArr = [[NSMutableArray alloc]init];
  [self addTableView];
  [self addData];
}
- (void)addTableView{
  UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, kScreenHeight) style:UITableViewStyleGrouped];
  self.tableView = tableView;
  tableView.delegate = self;
  tableView.dataSource = self;
  tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  [self.view addSubview:_tableView];
}
- (void)addData{
  self.titleArray = [NSArray arrayWithObjects:@"好友",@"陌生人", nil];
  self.friendsArray = [NSArray arrayWithObjects:
                       @[@"A",@"B",@"C",@"D",@"E",@"F"],
                       @[@"陌生1",@"陌生2",@"陌生3",@"陌生4",@"陌生5",@"陌生6",@"陌生7",@"陌生8",@"陌生9",@"陌生10",@"陌生11",@"陌生12",@"陌生13",@"陌生14"],nil];
}

#pragma mark --tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)section];
  NSInteger num ;
  //如果selectedArr不包含section,该分组返回number为0;
  if ([self.selectedArr containsObject:sectionStr]) {
    NSArray *arrayData = self.friendsArray[section];
    
    num = arrayData.count ;
  }else{
    num = 0;
  }
  return num;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
  view.backgroundColor = [UIColor whiteColor];
  UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5,kSCREENWIDTH-20 , 30)];
  titleLabel.text = self.titleArray[section];
  [view addSubview:titleLabel];
  
  //添加点击事件
  UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
  btn.tag = 100+section;
  [btn addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  [view addSubview:btn];
  return view;
}
- (void)viewBtnClick:(UIButton *)btn{
  NSString *string = [NSString stringWithFormat:@"%ld",btn.tag - 100];
  if ([self.selectedArr containsObject:string]) {
    [self.selectedArr removeObject:string];
  }else{
    [self.selectedArr addObject:string];
  }
  NSLog(@"selectedArr:%@",self.selectedArr);

  [_tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
  static NSString *cellID = @"testCellID";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

  }
  
  NSArray *arrayData = self.friendsArray[indexPath.section];
  
  if ([self.selectedArr containsObject:sectionStr]) {
    cell.textLabel.text = arrayData[indexPath.row];
  }
  return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 45;
}
@end

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

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

iOS 正则表达式判断手机号码、固话

本文主要介绍了iOS 正则表达式判断手机号码、固话,以及匹配是否是移动/联通/电信手机号的方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

运用iOS教你轻松制作音乐播放器

这篇文章主要教大家如何运用iOS轻松制作音乐播放器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 二维码扫描和应用跳转

本文讲解如何使用原生框架实现二维码扫描功能,并且进行扫描后的项目跳转。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS 定制多样式二维码

最常见的二维码功能包括信息获取、网站跳转、电商交易、手机支付等等,其拥有密度小、信息容量大、容错能力强、成本低、制作难度低等优点。在移动开发中,二维码的地位也越来越重要,掌握二维码的基本操作是重要的本领之一。本文将讲解iOS定制二维码的步骤与方法。
收藏 0 赞 0 分享

iOS获取当前app的设备名称和版本号等内容

本文主要介绍了iOS获取当前app的设备名称和版本号等内容的方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS页面跳转及数据传递(三种)

本文主要介绍了iOS页面跳转的三种方法及数据传递的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)

本文主要介绍了iOS常用小功能:获得屏幕图像,label的动态size,时间戳转化为时间,RGB转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理的方法。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS实现一个简易日历代码

本篇文章主要介绍了iOS实现一个简易日历代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS仿微信摇一摇动画效果加震动音效实例

这篇文章主要介绍了iOS仿微信摇一摇动画效果加震动音效实例,详细介绍了微信摇一摇功能的实现原理,非常具有实用价值,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS上下文实现评价星星示例代码

这篇文章主要介绍了iOS上下文实现评价星星的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享
查看更多