美文网首页
基于UITableView的下拉列表功能实现

基于UITableView的下拉列表功能实现

作者: HaloMartin | 来源:发表于2016-09-03 16:37 被阅读0次

    第一次在简书上写文章,一点点积累,牢记实际开发和项目中遇到的问题和解决方法,见证自己!
    上次在项目里遇到一个需求,要求按类型分组显示不同的设备信息,实现点击类型可以展开/缩回分组,网上虽然有方法,但是都是比较零散的,在这里自己写了一个。</br>
    我们来分析一下,需要哪些成员,

    • 首先,我们需要一个字典_sectionDetailDict,用于保存分组内容,key为分组类型;value为分组包含的内容,是一个数组;
    • 其次,我们需要一个字典_sectionOpenDict,用于保存分组展开/缩回信息;
    • 最后,我们需要一个数组_sectionArray,用于保存保存类型信息,可以对该数组进行排序,进而按照你想要的顺序显示。

    在这里,我们尽量使用了OC里自带的字典和数组,实际上,方法不止一个,我就曾经试过自定义一个对象,成员包含分组的展开/缩回状态,以及分组包含的内容信息,然后再以一个数组来存放该对象的实例,实际上的效果也还是可以的,缺点就是,在获取对应cell的信息时,需要循环去获取,不是很方便,使用现在这个方法的好处就是最大的利用了字典的便利。
    基于上面的分析,我先定义了三个成员,

    @interface MCDropdownListViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        UITableView* _tableV;//列表
        NSDictionary* _sectionDetailDict;//分组详细信息,key为当前分组头信息,必须是唯一的;value为数组,包含分组下的信息
        NSMutableDictionary* _sectionOpenDict;//分组展开/缩回信息
        NSArray* _sectionOrderArray;//分组顺序信息,包含分组顺序信息
    }
    

    接下来就实现UITableViewDataSource和UITableViewDelegate,这里只po出主要方法的实现,完整代码可在文章底部的链接中获取,

    - UITableViewDataSource

    我自定义了一个类型,用来表示当前分组的展开/缩回状态

    //自定义一个类型,用于表示列表的展开/缩回状态
    typedef NS_ENUM(NSUInteger,MCDropdownListSectionStatu) {
        MCDropdownListSectionStatuOpen = 1,
        MCDropdownListSectionStatuClose = 0,
    };
    

    个人觉得,最好把代码中没有特殊意义的数字和字符想办法以可读性高的方式呈现出来,必要时,还可以放在.h文件中,模仿苹果的风格!

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //先取section信息
        NSNumber* key = [_sectionOrderArray objectAtIndex:section];
        //再取section的展开/缩回状态,展开时,返回实际的分组数量;缩回时,返回0,以实现不展开
        MCDropdownListSectionStatu openOrNot = [[_sectionOpenDict objectForKey:key] unsignedIntegerValue];
        if (openOrNot == MCDropdownListSectionStatuOpen) {
            //列表展开,取分组实际数量,并返回
            NSArray* arr = [_sectionDetailDict objectForKey:key];
            return arr.count;
        }
        return 0;
    }
    
    -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView* headerView = nil;
        headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, HeightForHeader)];
        UIButton* btn = [[UIButton alloc] initWithFrame:headerView.frame];
        [headerView addSubview:btn];
        //分组名
        UILabel* sectionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, (btn.bounds.size.width-10)*2/3, btn.bounds.size.height)];
        sectionNameLabel.text = [_sectionOrderArray objectAtIndex:section];
        [btn addSubview:sectionNameLabel];
        //分组下Cell的数量
        UILabel* qtyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (btn.bounds.size.width-10)/3, btn.bounds.size.height)];
        qtyLabel.center = CGPointMake(tableView.bounds.size.width-10-qtyLabel.bounds.size.width/2, btn.bounds.size.height/2);
        qtyLabel.text = [NSString stringWithFormat:@"%d个",(int)[[_sectionDetailDict objectForKey:[_sectionOrderArray objectAtIndex:section]] count]];
        qtyLabel.textAlignment = NSTextAlignmentRight;
        [btn addSubview:qtyLabel];
        //添加点击处理
        [btn addTarget:self action:@selector(onExpandSection:) forControlEvents:UIControlEventTouchUpInside];
        //标记,用于处理点击事件
        btn.tag = section;
        //
        return headerView;
    }
    

    - action

    实现onExpandSection方法

    -(void)onExpandSection:(UIButton*)button
    {
        //获取分组信息
        NSString* section = [_sectionOrderArray objectAtIndex:button.tag];
        //获取分组展开/缩回的状态
        MCDropdownListSectionStatu openOrNot = [[_sectionOpenDict objectForKey:section] unsignedIntegerValue];
        if (MCDropdownListSectionStatuClose == openOrNot) {
            NSLog(@"点击事件,展开分组:%@",section);
            //原先是缩回的,现在展开
            [_sectionOpenDict setObject:[NSNumber numberWithUnsignedInteger:MCDropdownListSectionStatuOpen] forKey:section];
        }else {
            NSLog(@"点击事件,缩回分组:%@",section);
            //原先是展开的,现在缩回
            [_sectionOpenDict setObject:[NSNumber numberWithUnsignedInteger:MCDropdownListSectionStatuClose] forKey:section];
        }
        //刷新列表,展示结果(刷新单独的section会有卡顿的情况,我这直接改用刷新整个列表)
    //    [_tableV reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationNone];
        [_tableV reloadData];
    }
    

    效果:

    QQ20160903-0.png
    总结,方法不是唯一的,希望自己能有更多不同的想法,时间允许的话,也想实践一下,这里还有很多不足的地方,功能也是很简单,后续尝试一些新功能进去。第一次写文章,写的有问题的地方欢迎指正!
    代码链接:https://github.com/HaloMartin/DropdownList

    相关文章

      网友评论

          本文标题:基于UITableView的下拉列表功能实现

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