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

 更新时间:2018年01月26日 16:51:36   作者:勤劳的小QQ  
本文介绍了iOS开发之tableView cell的展开收回功能实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、实现方法

例如好友分组,分为好友和陌生人两组,实现点击好友和陌生人展开或收回该分组对应的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代码笔记之勾选"记住密码"整体button

    这篇文章主要为大家详细介绍了IOS实现勾选"记住密码"整体button效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • OC runtime学习笔记之关联对象

    OC runtime学习笔记之关联对象

    这篇文章主要介绍了OC runtime学习笔记之关联对象的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • iOS开发中ViewController的页面跳转和弹出模态

    iOS开发中ViewController的页面跳转和弹出模态

    这篇文章主要介绍了iOS开发中ViewController的页面跳转和弹出模态,ViewController是MVC开发模式中一个重要的类,需要的朋友可以参考下
    2015-10-10
  • iOS mobileconfig配置文件进行签名的配置方法

    iOS mobileconfig配置文件进行签名的配置方法

    这篇文章主要介绍了iOS mobileconfig配置文件进行签名的配置方法,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • iOS支付宝支付方法详解

    iOS支付宝支付方法详解

    这篇文章主要为大家详细介绍了iOS支付宝支付方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • iOS使用CIFilter生成二维码

    iOS使用CIFilter生成二维码

    这篇文章主要介绍了iOS使用CIFilter生成二维码,二维码的生成和读取只需要使用Core Image框架和AVFoundation框架就能轻松实现。在这里,我们主要介绍二维码的生成。有兴趣的可以了解一下
    2017-12-12
  • iOS模仿电子书首页实现书架布局样式

    iOS模仿电子书首页实现书架布局样式

    这篇文章主要为大家详细介绍了iOS实现类似电子书首页效果样式,实现书架布局样式,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS 组件化初步构思

    iOS 组件化初步构思

    这篇文章主要介绍了iOS组件化初步构思,并对iOS组件化常用方式的讨论进行了方案分析,以便帮助大家对ios组件化有一个深刻的了解
    2023-03-03
  • iOS中UITableView使用的常见问题总结

    iOS中UITableView使用的常见问题总结

    这篇文章主要总结了iOS中UITableView使用的常见问题,其中包括如何设置headerView以及其高度、去掉多余cell的分割线 以及如何设置section数、行数等一系列的问题,文中介绍的更详细,需要的朋友们下面来一起看看详细介绍吧。
    2017-03-03
  • iOS中id类型的理解及底层原理详解

    iOS中id类型的理解及底层原理详解

    这篇文章主要给大家介绍了关于iOS中id类型的理解以及底层原理的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07

最新评论