简单的下拉菜单 -- 表格

4 篇文章 0 订阅
1 篇文章 0 订阅

1、在 AppDelegate.m 中包装导航控制器

导入头文件

#import "ViewController.h"

在 didFinishLaunchingWithOptions 方法中更改主窗口

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];

    return YES;
}

2、 在 ViewController.m 中
定义宏

// 整个屏幕的宽
#define WIDTH self.view.frame.size.width
// 整个屏幕的高
#define HEIGHT self.view.frame.size.height

设置表格的协议

<UITableViewDelegate, UITableViewDataSource>
// 定义属性
@property (nonatomic ,strong) UITableView *addMenuTableView;   // + 号下拉表格
@property (nonatomic ,strong) UIButton *addBtn; // + 按钮

在 viewDidLoad 中设置

- (void)viewDidLoad {
    [super viewDidLoad];

    // 视图的背景颜色
    self.view.backgroundColor = [UIColor whiteColor];

    // 导航条控件颜色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

    // 创建按钮添加到右侧导航条
    _addBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _addBtn.frame = CGRectMake(self.view.frame.size.width - 50, 5, 30, 30);
    [_addBtn setTitle:@"+" forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(didClickRightNavAddBtn:) forControlEvents:UIControlEventTouchUpInside];
    _addBtn.titleLabel.font = [UIFont systemFontOfSize:30];
    // 将按钮添加到导航条
    [self.navigationController.navigationBar addSubview:_addBtn];

    // 创建 下拉小表格
    _addMenuTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH - 10, 68, 0.1, 0.1) style:UITableViewStylePlain];
    _addMenuTableView.delegate = self;
    _addMenuTableView.dataSource = self;
    _addMenuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _addMenuTableView.rowHeight = 50;
    [_addMenuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"menuTableViewcell"];
    [self.view addSubview:_addMenuTableView];
}

右侧导航条按钮的点击事件

// 右侧导航条按钮的点击事件
-(void)didClickRightNavAddBtn:(UIButton *)sender
{
     //判断 如果是选中状态
    if (sender.selected == YES) {

        // 点击则变成不选中状态
        sender.selected = NO;
        // 不显示状态时表格不显示
        [UIView animateWithDuration:0.5 animations:^{

            _addMenuTableView.frame = CGRectMake(WIDTH - 30, 68, 0.1, 0.1);
        }];

    }else if (sender.selected == NO){   // 如果是不选中状态

        // 点击则变成选中状态
        sender.selected = YES;
        // 选中状态表格显示
        [UIView animateWithDuration:0.5 animations:^{

            _addMenuTableView.frame = CGRectMake(WIDTH - 160, 68, 150, 100);
            [_addMenuTableView reloadData];
        }];
    }

}

数据源方法

#pragma mark TableView DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuTableViewcell"];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"menuTableViewcell"];
    }

    NSArray *imgArr = @[@"shangchuanVideo",@"musicFu"];
    NSArray *titArr = @[@"上传视频",@"上传音频"];
    cell.imageView.image = [UIImage imageNamed:imgArr[indexPath.row]];
    cell.textLabel.text = titArr[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor redColor];

    return cell;
}

点击屏幕空白处隐藏表格

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 点击则变成不选中状态
    _addBtn.selected = NO;
    // 不显示状态时表格不显示
    [UIView animateWithDuration:0.5 animations:^{

        _addMenuTableView.frame = CGRectMake(WIDTH - 30, 68, 0.1, 0.1);
    }];
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值