虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS开发之tableView cell的展开收回功能实现代码

iOS开发之tableView cell的展开收回功能实现代码
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

本文介绍了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 UITableViewCell详解及按钮点击事件处理实例
  • 详解iOS开发中UITableview cell 顶部空白的多种设置方法
  • IOS UITableView和UITableViewCell的几种样式详细介绍
  • iOS App中UITableView左滑出现删除按钮及其cell的重用
  • 全面解析iOS应用中自定义UITableViewCell的方法
  • iOS中UITableView Cell实现自定义单选功能
  • iOS中tableView cell分割线的一些设置技巧
  • iOS App开发中使用及自定义UITableViewCell的教程
  • 详解iOS tableViewCell自适应高度 第三发类库
  • 详解ios中自定义cell,自定义UITableViewCell
相关热词搜索: tableview中cell展开 iOS tableView cell展