我们在学习的过程中,可能会遇到需要添加下拉菜单功能的时候,有的小伙伴可能一时半会想不出来怎样实现,那么我今天就来给大家分享一下我的方法
先看一下效果图
左边是一个UIButton,点击前是如下效果
这是点击星期三后的效果
这样,就实现了点击下拉菜单的实现
我说一下原理
1.先设置一下tableView的尺寸,然后在它旁边添加一个按钮,在按钮被点击后,将tableView的高度设置为0,这样就实现了下拉菜单的隐藏。
2.将label的text先设为空,在点击cell之后,将label的text设置为当前cell的text,并且将下拉菜单收回(将tableView的高度设置为0)。在点击按钮之后,将高度又变为为原来的高度,这样就会回弹出来。
是不是很简单呢?
下面分享一下代码
ViewController.m
#import "ViewController.h"
@interface ViewController ()
<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, copy)NSArray *dataArray;
@property(nonatomic, strong)UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dataArray = @[@"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", @"星期日"];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 130, 130, 120) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(70, 100, 30, 30);
_button.backgroundColor = [UIColor yellowColor];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[_button setTitle:@"收" forState:UIControlStateNormal];
[_button setTitle:@"弹" forState:UIControlStateSelected];
[_button addTarget:self action:@selector(clickToPush:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
_selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 130, 30)];
_selectLabel.text = @" ";
[_selectLabel.layer setCornerRadius:0];
[_selectLabel.layer setBorderWidth:1];
[_selectLabel.layer setBorderColor:[UIColor blackColor].CGColor];
[self.view addSubview:_selectLabel];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30;
}
- (void)clickToPush:(UIButton *)btn
{
btn.selected = !btn.selected;
if (btn.selected == YES) {
_tableView.frame = CGRectMake(100, 130, 130, 0);
}
else {
_tableView.frame = CGRectMake(100, 130, 130, 120);
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectLabel.text = _dataArray[indexPath.section];
_tableView.frame = CGRectMake(100, 130, 130, 0);
_button.selected = !_button.selected;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
}
cell.backgroundColor = [UIColor orangeColor];
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = _dataArray[indexPath.section];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
@end