iOS 简易的实现类似QQ好友菜单

使用tableview实现这种功能

需要有以下三个属性,省略很多代码,这里介绍核心代码。

//用于存放每一个好友

@property (nonatomic,strong)NSMutableArray * dataSource;

//用于记住组的展开和闭合状态

@property(nonatomic,strong)NSMutableArray * groupState;

@property (nonatomic,strong)UITableView * tableView;


初始化小组都是关闭

/*懒加载 用于记住组的展开和闭合状态(初始化为关闭状态)*/

- (NSMutableArray *)groupState

{

    if (!_groupState) {

        _groupState = [NSMutableArraynew];

        

        for (int i =0; i <self.dataSource.count; i++) {

//            [_groupState addObject:[NSNumber numberWithBool:NO]];

            //简写为

            [_groupStateaddObject:@NO];

        }

    }

    

    return_groupState;

}


/*制作假数据 datasource里面存放了几个数组,每个数组代表每个好友小组*/

- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArraynew];


        NSArray * pfp = [[NSBundlemainBundle] pathsForResourcesOfType:@"plist"inDirectory:nil];

        

        for (NSString * ppin pfp) {

            if ([pphasSuffix:@"Info.plist"]) {

                continue;

            }

            

            NSMutableArray * array = [NSMutableArrayarrayWithContentsOfFile:pp];

            

            

            [_dataSourceaddObject:array];

        }

    }



    return_dataSource;

}


#pragma - mark DataSource

/*返回有多少个好友分组*/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    returnself.dataSource.count;

}

/*返回每个组有多少好友*/

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

{

   //当小组状态为关闭时,返回0个好友(即小组为关闭状态)

    if ([self.groupState[section]boolValue] == NO) {

        return0;

    }

    return [self.dataSource[section]count];

}


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

{

    UIView * view = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 1,1)];

    view.backgroundColor = [UIColororangeColor];

    

    //实现折叠效果

    //1.在每一组的头上添加一个按钮,好友的展开关闭按钮

    UIButton * btn = [UIButtonbuttonWithType:UIButtonTypeSystem];

    btn.frame =CGRectMake(0,0, 50,50);

    [btn setTitle:@"➡️"forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

    btn.tag = section +100;

    [view addSubview:btn];

    

    //2.根据组的折叠状态,设置btn的指示方向

    if ([self.groupState[section]boolValue] == YES) {

        btn.transform =CGAffineTransformMakeRotation(M_PI/2.0);

    }

    return view;

}


/*小组展开关闭按钮点击事件*/

- (void)btnClicked:(UIButton *)btn

{

    //取到按钮所在的section的序号

    NSInteger sectionIndex = btn.tag -100;

    //修改groupState标记数组里面对应序号的Bool变量

    // 1.取出变量原来的值

    BOOL oldBool = [self.groupState[sectionIndex]boolValue];

    // 2. 将原来的值取反

    NSNumber * newValue =

    [NSNumbernumberWithBool:!oldBool];

    // 3. 将取反的值替换数组中对应序号的对象

    [self.groupStatereplaceObjectAtIndex:sectionIndexwithObject:newValue];

    

    //4.需要重新加载TableView

    //  通过调用UITableView reloadData方法,

  //    [self.tableView reloadData];


    //刷新指定的组,并且可以加上动画效果

    [self.tableViewreloadSections:[NSIndexSetindexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];    

}


大致就是这样的思路,省略了一些代码,只是一个简单的demo。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值