iOS-tableView点击下拉菜单

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

 

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *sectionArray;//section标题

@property(nonatomic, strong)NSMutableArray *rowInSectionArray;//section中的cell个数

@property(nonatomic, strong)NSMutableArray *selectedArray;//是否被点击

 

 

@end

 

@implementation ViewController

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,40 , self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain];

    _tableView.tableFooterView = [[UIView alloc]init];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

    

    

    _sectionArray = [NSMutableArray arrayWithObjects:@"第一组",@"第二组",@"第三组",@"第四组", nil];//每个分区的标题

    _rowInSectionArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"5",@"6", nil];//每个分区中cell的个数

    _selectedArray = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0", nil];//这个用于判断展开还是缩回当前sectioncell

}

 

#pragma mark section的个数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return _sectionArray.count;

}

 

#pragma mark cell的行数

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

{

    //判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0.

    if ([_selectedArray[section] isEqualToString:@"1"]) {

        return [_rowInSectionArray[section]integerValue];

    }

    return 0;

}

 

#pragma mark cell的内容

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

{

    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    cell.textLabel.text = _sectionArray[indexPath.section];

    return cell;

}

 

#pragma cell的高度

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

{

    return 40;

}

 

#pragma mark - section内容

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

{

    //每个section上面有一个button,button一个tag,用于在点击事件中改变_selectedArray[button.tag - 1000]的值

    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 40)];

    sectionView.backgroundColor = [UIColor grayColor];

    UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];

    sectionButton.frame = sectionView.frame;

    [sectionButton setTitle:[_sectionArray objectAtIndex:section] forState:UIControlStateNormal];

    [sectionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    sectionButton.tag = 1000 + section;

    [sectionView addSubview:sectionButton];

    return sectionView;

}

#pragma mark button点击方法

-(void)buttonAction:(UIButton *)button

{

    if ([_selectedArray[button.tag - 1000] isEqualToString:@"0"]) {

        

//        打开这段代码可以实现只展开一个列表的功能

//        for (NSInteger i = 0; i < _sectionArray.count; i++) {

//            [_selectedArray replaceObjectAtIndex:i withObject:@"0"];

//            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];

//        }

 

        

        //如果当前点击的section是缩回的,那么点击后就需要把它展开,_selectedArray对应的值为1,这样当前section返回cell的个数就变为真实个数,然后刷新这个section就行了

        [_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"1"];

        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];

    }

    else

    {

        //如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了

        [_selectedArray replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];

        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];

    }

}

 

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_tableViewDemo1

原文地址:https://www.cnblogs.com/zhang-kiwi/p/5031473.html