您的位置:首页 > 其它

实现下拉列表的一种方法

2016-04-13 17:31 465 查看
如下图,这种点击可以展开详细列表的表视图,下面阐释一种简单的实现方法:



实现思路如下:

首先,给表视图初始化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];

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: