美文网首页
好友分组的点开与点关

好友分组的点开与点关

作者: 会飞的夜良 | 来源:发表于2018-01-15 17:50 被阅读0次

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

方式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

相关文章

  • 好友分组的点开与点关

    效果:(界面有点丑,请别介意) 方式1:字典存状态,通用所有 #import "XHGPSDemoViewCont...

  • 好友分组

    随着即时通讯软件的发展和更迭,周围好像已经很少有人在用QQ聊天,至少对我而言,它仅仅是工作中的一个传输工具。好友列...

  • 【15】人际关系三大法则

    今天的主要任务是加好友、分组、点赞、评论及转发。老师说了很多如何让别人关注你的方法! 我突然想到了,我之前学过的关...

  • QQ好友分组

    好久没看过QQ好友列表。 今天为了给闺蜜发个文件,点开了QQ,我把她放在了分组命名为“26人”的组里面。 为什么是...

  • QQ列表实现 -- 黄克刚

    实现效果如图所示,点击好友分组展开分组下的所有好友。再次点击收回分组。当一个分组处于展开状态时点击其他分组,展开点...

  • 您的好友分组可见

    分组可见这事儿一般是由当事人暗中操作的,具有主观能动性,被分组的人基本上都被蒙在鼓里。所以有时候你翻看好友列表会发...

  • mysql跨日期按时间段分组

    需求:将昨天凌晨4点到第二天的凌晨4点当作一天来进行分组。 分析:正常情况下按日期分组都是从0点开始,所以直接gr...

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

    一、实现方法 例如好友分组,分为好友和陌生人两组,实现点击好友和陌生人展开或收回该分组对应的cell的功能。实现:...

  • 关于朋友圈好友分组技巧

    朋友圈好友的细化、精准分组,对于维护客情和发圈是必做的基础。朋友圈好友如何分组呢? 1、星标:每天必看:这类好友,...

  • 【03】我为谁写日记

    今天是日记班的第三课,老师讲的是如何加好友,分组,备注,特别关心,点赞,@好友,评论,分享,转发,我们的上...

网友评论

      本文标题:好友分组的点开与点关

      本文链接:https://www.haomeiwen.com/subject/tmdqpxtx.html