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

所属分类: 软件编程 / IOS 阅读数: 1786
收藏 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中给自定义tabBar的按钮添加点击放大缩小的动画效果

这篇文章主要介绍了iOS中给自定义tabBar的按钮添加点击放大缩小的动画效果的相关资料,非常不错,具有参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解iOS使用Keychain中的kSecClassGenericPassword存储数据

iOS设备中的Keychain是一个安全的存储容器,本篇文章主要介绍了iOS使用Keychain中的kSecClassGenericPassword存储数据,有兴趣的可以了解一下。
收藏 0 赞 0 分享

详解IOS四种保存数据的方式

本篇文章主要介绍了OS四种保存数据的方式,现在分享给大家,也给大家做个参考。感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

iOS图片界面翻页切换效果

这篇文章主要为大家详细介绍了iOS图片界面翻页切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

IOS中对Url进行编码和解码示例

本篇文章主要介绍了IOS中对Url进行编码和解码示例,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS实现圆角箭头矩形的提示框

不知道大家发现了没,在现在的很多App中常使用圆角箭头矩形, 如微博分组提示框, 地图坐标显示点等。iPad 中有 UIPopoverController 类供开发使用, iPhone中就需要开发人员定制了。那么下面这篇文中就来聊聊定制圆角箭头矩形提示框,有需要的朋友们可以参考借
收藏 0 赞 0 分享

iOS将视频录像切成一张张缩略图

这篇文章主要为大家详细介绍了iOS将视频录像切成一张张缩略图的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS获取验证码倒计时效果

这篇文章主要为大家详细介绍了iOS获取验证码倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多