实现下拉列表的一种方法

如下图,这种点击可以展开详细列表的表视图,下面阐释一种简单的实现方法:

实现思路如下:

首先,给表视图初始化N个分区,给每个分区用一个布尔值参数来判断该分区是否需要展开详细列表,初始的时候,每个分区返回的cell个数是0,即不展开。

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

    return _provinceArray.count;

}


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

    

    NSInteger num = 0;

    

    NSArray *citiArray = _citiesArray[section];

    

    if (!_isOpen[section]) {

        num=0;

    }else{

        num = citiArray.count;

    }

    return num;

}

然后,给表视图返回分区头的方法中添加具有点击手势的UIView,来实现点击该分区头时展开列表的功能。分区头的高度需要设置一下,否则不显示。

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

    UIView *view;


    view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 44)];

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 44)];

    titleLabel.font = [UIFont systemFontOfSize:15];

    NSString*provinceStr = _provinceArray[section];

    titleLabel.text = provinceStr;

        

    [view addSubview:titleLabel];

    view.backgroundColor = [UIColor whiteColor];

    

        view.tag = section +10;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ggShouFang:)];

        [view addGestureRecognizer:tap];

        

        UIButton *jiantouBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [jiantouBtn setFrame:CGRectMake(DEVICE_WIDTH-44, 0, 44, 44)];

        jiantouBtn.userInteractionEnabled = NO;

        [view addSubview:jiantouBtn];

        

        if ( !_isOpen[view.tag-10]) {

            [jiantouBtn setImage:[UIImage imageNamed:@"buy_jiantou_d.png"] forState:UIControlStateNormal];

        }else{

            [jiantouBtn setImage:[UIImage imageNamed:@"buy_jiantou_u.png"] forState:UIControlStateNormal];

        }

    }


    return view;

}


最后实现分区头的点击手势的点击方法:点击时对应改变相应分区用于判断是否展开的布尔值参数。刷新tableview或者刷新对应的分区的时候即可展示下拉菜单详情。

-(void)ggShouFang:(UIGestureRecognizer*)ges{

    

    _isOpen[ges.view.tag-10]=!_isOpen[ges.view.tag-10];

    

    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:ges.view.tag-10];

    [_tabelView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

    

}







  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值