tableviewCell折叠状态1

//

//  LHQReportOnTheUseOfFundsCtrl.m

//  11 - 投资管理 - 李洪强

//  资金使用情况汇报

//  Created by vic fan on 16/4/12.

//  Copyright © 2016 李洪强. All rights reserved.

//

 

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define SCREENH [UIScreen mainScreen].bounds.size.height

 

#import "LHQReportOnTheUseOfFundsCtrl.h"

 

#import "LHQContentViewCell.h"

 

#import "LHQDelegateModel.h"

 

#import "NSString+Frame.h"

 

#import "LHQCustomHeader.h"

 

@interface LHQReportOnTheUseOfFundsCtrl ()<UITextViewDelegate>

 

@property(nonatomic)NSMutableArray *dataArr;

 

//记录所有分组的折叠状态

@property(nonatomic)NSMutableArray *closeArr;

 

 

@end

 

@implementation LHQReportOnTheUseOfFundsCtrl

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"资金使用情况汇报";

    

    

    [self.tableView registerNib:[UINib nibWithNibName:@"LHQContentViewCell" bundle:nil] forCellReuseIdentifier:@"LHQContentViewCell"];

    

    [self createData];

    

}

 

-(void)viewWillDisappear:(BOOL)animated

{

    

    _dataArr = nil;

    _closeArr = nil;

   

    

}

 

//数据

- (void)createData{

    

    NSArray *tmpArray = @[@"201511月资金情况汇报",@"201510月资金情况汇报",@"20159月资金情况汇报",@"20158月资金情况汇报",@"20157月资金情况汇报",@"20156月资金情况汇报"];

    

    //循环给标题赋值

    for (int i=0; i<6; i++) {

       

        LHQDelegateModel *model = [[LHQDelegateModel alloc] init];

        model.titleName = tmpArray[i];

        

        //循环给每一个cell里面的数据数组赋值

        for (int j=0; j<4; j++) {

            [model.contentArr addObject:@"13bcbduaib"];

        }

        

    //把模型model放到数据数组中

        

        [self.dataArr addObject:model];

        [self.closeArr addObject:[NSNumber numberWithBool:YES]];

        

    }

 

    

    [self.tableView reloadData];

    

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - Table view data source

//几组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    

    return _dataArr.count;

    

    

}

 

//几行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //如果是关闭状态

    //objectAtIndex:(NSUInteger)index : 获取数组中索引为index的元素

    if ([[_closeArr objectAtIndex:section] boolValue]) {

        return 0;

    }

    

   // 如果是打开的状态--> 返回每一组只有一行

    return 1;

}

 

//每一行显示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    LHQContentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHQContentViewCell" forIndexPath:indexPath];

    

    if (!cell) {

        cell=[[LHQContentViewCell alloc] init];

    }

    LHQDelegateModel *model = _dataArr[indexPath.section];

 

    [cell customedWithModel:model];

    

    // cell.backgroundColor = [UIColor blueColor];

    return cell;

    

}

 

 

//每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 223;

    

}

 

//定义段头

 

//头部高度

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 60;

}

 

//头部视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{   //btn当作段头的view

 

    LHQDelegateModel *model = _dataArr[section];

    

    

    LHQCustomHeader *headerView = [[[NSBundle mainBundle] loadNibNamed:@"LHQCustomHeader" owner:self options:nil] firstObject];

    //定义一个变量是打开的状态

    

    //indexOfObject 对象是否存在数组中,如果存在返回对象所在的下标

    NSInteger numx = [self.closeArr indexOfObject:[NSNumber numberWithBool:0]];

   

    //如果组是打开的状态

    if (numx == section) {

        

        //设置箭头为向上的

        [headerView setImageWithName:@"Snip20160413_9"];

        

    }else{

    //否则还是原来的

        [headerView setImageWithName:@"Snip20160413_8"];

    }

    

    //headView绑定tag

    headerView.tag = section + 400;

    //给标题赋值

    headerView.titleLable.text = model.titleName;

    

    

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnClick:)];

    

    [headerView addGestureRecognizer:tapGR];

    

    CALayer *layer = [headerView layer];

    [layer setMasksToBounds:YES];//设置边框可见

    layer.borderColor = [[UIColor lightGrayColor] CGColor];

    [layer setBorderWidth:0.5];

    //Border: 边境

    return headerView;

}

 

 

 

//点击选择

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    // [self.delegate  sendLocation:[_dataArr[indexPath.section] objectAtIndex:indexPath.row]];

    

    // [self dismissViewControllerAnimated:YES completion:nil];

    

    

}

 

//点击头部

-(void)btnClick:(UITapGestureRecognizer *)tapGR

{

    //找到对应的折叠状态

    //此时是关闭状态

    BOOL isClose = [[_closeArr objectAtIndex:tapGR.view.tag-400] boolValue];

    

    //(要点: 点击某一行cell,如果当前cell的状态是关闭的,点击的时候就把当前的cell切换到打开的状态,同时把其他所有的cell的状态都关闭

    // 如果当前的cell状态是打开的,点击的时候,把所有的状态改成关闭状态)

//-----------------------------------------------------------

    for (int i=0; i<6; i++) {

        

        //如果点击的tag值在范围之内

        if (i == tapGR.view.tag - 400) {

            //当前点击的cell的索引正好是关闭的那个状态,什么都不做

            //如果打开的是当前的某一个cell,什么都不做

            //如果是其他的,让其他的关闭

        }else{

            //没打开的状态

            

    [self.closeArr replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:YES]];

           

 

            }

        }

//----------------------------------------------------------

    //修改折叠状态

    //把关闭的那个索引的cell变成打开的状态

    [_closeArr replaceObjectAtIndex:tapGR.view.tag-400 withObject:[NSNumber numberWithBool:!isClose]];

   

 

    [self.tableView reloadData];

    

}

 

#pragma mark - textView的代理方法

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    

    NSLog(@"url :%@",URL);

    

    NSLog(@"111");

    

    if ([[URL scheme] isEqualToString:@"username"]) {

        NSString *username = [URL host];

        NSLog(@"username :%@",username);

        return NO;

    }

    return YES;

}

 

 

#pragma mark - 懒加载

-(NSMutableArray *)dataArr

{

    if (!_dataArr) {

        _dataArr = [[NSMutableArray alloc] init];

        

    }

    return _dataArr;

    

}

 

-(NSMutableArray *)closeArr

{

    if (!_closeArr) {

        _closeArr = [[NSMutableArray alloc] init];

    }

    return _closeArr;

    

}

 

 

 

@end

原文地址:https://www.cnblogs.com/LiLihongqiang/p/5534599.html