好友分组的点开与点关

效果:(界面有点丑,请别介意)


好友分组的点开与点关_第1张图片

方式1:字典存状态,通用所有



#import "XHGPSDemoViewController.h"@interface XHGPSDemoViewController ()/** tableView*/

@property (nonatomic, strong) UITableView * tableView;

/** 数据数组*/

@property (nonatomic, strong) NSMutableArray * dataArray;

/** 记录组的开关*/

@property (nonatomic, strong) NSMutableDictionary * sectionStateDic;

@end

@implementation XHGPSDemoViewController

#pragma mark - LazyLoad 懒加载

- (UITableView *)tableView {

    if (_tableView == nil) {

        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

        _tableView.tableFooterView = [UIView new];

        [self.view addSubview:_tableView];

    }

    return _tableView;

}

- (NSMutableArray *)dataArray {

    if (_dataArray == nil) {

        _dataArray = [NSMutableArray new];

    }

    return _dataArray;

}

- (NSMutableDictionary *)sectionStateDic {

    if (_sectionStateDic == nil) {

        _sectionStateDic = [NSMutableDictionary new];

    }

    return _sectionStateDic;

}

#pragma mark - System Method 系统方法

- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor whiteColor];

    [self configSubViews];

    [self transData];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - Custom Method 自定义方法

/** 配置子视图、子控件 */

- (void)configSubViews {


    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];


    self.tableView.tableHeaderView = headerView;

}

#pragma mark - TableView DataSource 数据源方法(TableVieW)

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

    NSArray *listArray = self.dataArray[section];

    BOOL isHiden = [[self.sectionStateDic objectForKey:@(section)] boolValue];


    if (isHiden) {

        return 0;

    }

    return listArray.count;

}

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


    return self.dataArray.count;

}

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


    static NSString *cellId = @"CellId";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];


    if (cell == nil) {

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

    }

    NSArray *listArray = self.dataArray[indexPath.section];


    cell.textLabel.text = listArray[indexPath.row];


    return cell;

}

#pragma mark - TableView Delegate 代理(TableVieW)

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


    return 50;

}

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


    UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [sectionBtn setTitle:@"点下就关了" forState:UIControlStateNormal];

    [sectionBtn setTitle:@"点下就开了" forState:UIControlStateSelected];

    [sectionBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

    sectionBtn.backgroundColor = [UIColor cyanColor];

    [sectionBtn addTarget:self action:@selector(sectionBtnAction:) forControlEvents:UIControlEventTouchUpInside];


    /** 设置状态*/

    sectionBtn.tag = section + 1000;

    sectionBtn.selected = [[self.sectionStateDic objectForKey:@(section)] boolValue];

    return sectionBtn;

}

#pragma mark - NetWork 网络请求

/** 请求数据 */

- (void)transData {

    /** 数据数组*/

    NSArray *firstSectionArray = @[@"00",@"01",@"02",@"03",@"04",@"05",@"06"];

    NSArray *secSectionArray = @[@"10",@"11",@"12",@"13",@"14"];

    NSArray *thirdSectionArray = @[@"20",@"21",@"22",@"23",@"24",@"25"];


    /** 添加数据*/

    [self.dataArray addObject:firstSectionArray];

    [self.dataArray addObject:secSectionArray];

    [self.dataArray addObject:thirdSectionArray];


    /** 状态*/

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

        [self.sectionStateDic setObject:@(YES) forKey:@(i)];

    }


    [self.tableView reloadData];

}

#pragma mark - Action 响应事件

- (void)sectionBtnAction:(UIButton *)btn {

    btn.selected = !btn.selected;

    NSInteger section = btn.tag - 1000;

    [self.sectionStateDic setObject:@(btn.selected) forKey:@(section)];

    [self.tableView reloadData];

}

@end



方式2:模型存状态,通用模型



#import@class List;@interface XHGPSDemoModel : NSObject/** 类型*/@property (nonatomic, copy) NSString * type;/** 分类名字*/@property (nonatomic, copy) NSString * classifyName;/** 模型数组*/@property (nonatomic, copy) NSArray *list;

/** 是否关*/

@property (nonatomic, assign) BOOL isClose;

@end

@interface List : NSObject

/** 名字*/

@property (nonatomic, copy) NSString * name;

/** 图片地址*/

@property (nonatomic, copy) NSString * imgUrl;

@end



#import "XHGPSDemoViewController.h"#import "XHGPSDemoModel.h"@interface XHGPSDemoViewController ()/** tableView*/

@property (nonatomic, strong) UITableView * tableView;

/** 数据数组*/

@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation XHGPSDemoViewController

#pragma mark - LazyLoad 懒加载

- (UITableView *)tableView {


    if (_tableView == nil) {

        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

        _tableView.tableFooterView = [UIView new];

        [self.view addSubview:_tableView];

    }

    return _tableView;

}

- (NSMutableArray *)dataArray {

    if (_dataArray == nil) {

        _dataArray = [NSMutableArray new];

    }

    return _dataArray;

}

#pragma mark - System Method 系统方法

- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor whiteColor];

    [self configSubViews];

    [self transData];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - Custom Method 自定义方法

/** 配置子视图、子控件 */

- (void)configSubViews {


    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];


    self.tableView.tableHeaderView = headerView;

}

#pragma mark - TableView DataSource 数据源方法(TableVieW)

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

    XHGPSDemoModel *model = self.dataArray[section];

    BOOL isHiden = model.isClose;


    if (isHiden) {

        return 0;

    }

    return model.list.count;

}

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


    return self.dataArray.count;

}

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


    static NSString *cellId = @"CellId";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];


    if (cell == nil) {

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

    }

    XHGPSDemoModel *model = self.dataArray[indexPath.section];


    List *listModel = model.list[indexPath.row];


    cell.textLabel.text = listModel.name;


    return cell;

}

#pragma mark - TableView Delegate 代理(TableVieW)

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


    return 50;

}

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


    UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [sectionBtn setTitle:@"点下就关了" forState:UIControlStateNormal];

    [sectionBtn setTitle:@"点下就开了" forState:UIControlStateSelected];

    [sectionBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

    sectionBtn.backgroundColor = [UIColor cyanColor];

    [sectionBtn addTarget:self action:@selector(sectionBtnAction:) forControlEvents:UIControlEventTouchUpInside];


    /** 设置状态*/

    sectionBtn.tag = section + 1000;


    XHGPSDemoModel *model = self.dataArray[section];

    sectionBtn.selected = model.isClose;

    return sectionBtn;

}

#pragma mark - NetWork 网络请求

/** 请求数据 */

- (void)transData {


    /** 状态 & 数据*/

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


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

        NSMutableArray *listArray = [NSMutableArray new];

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


            List *listModel = [[List alloc] init];

            listModel.name = [NSString stringWithFormat:@"第%ld组 ,第%ld个",i,j];

            [listArray addObject:listModel];

        }

        /** 默认关*/

        model.isClose = YES;

        model.list = listArray;

        [self.dataArray addObject:model];

    }


    [self.tableView reloadData];

}

#pragma mark - Action 响应事件

- (void)sectionBtnAction:(UIButton *)btn {


    btn.selected = !btn.selected;

    NSInteger section = btn.tag - 1000;


    XHGPSDemoModel *model = self.dataArray[section];

    model.isClose = btn.selected;


    [self.tableView reloadData];

}

@end

你可能感兴趣的:(好友分组的点开与点关)