iOS

  • 先上效果图

  • 上图设置的是点击cell后隐藏弹出的菜单,或者拖拽tableview隐藏弹出菜单, 或者点击当前页面的cell的时候隐藏.

  • 自己可以动态设置弹出框的高度.

  • 创建FMenuAlert继承UIView

  • .h文件



//
//  FMenuAlert.h
//  get_loc_taped
//
//  Created by 裴波波 on 2018/1/5.
//  Copyright © 2018年 裴波波. All rights reserved.
//  下拉弹框的一个view

#import <UIKit/UIKit.h>

@interface FMenuAlert : UIView
// 显示字体设置
@property(nonatomic,assign)UIFont * cusFont;

/**
 点击回调,返回所点的角标以及点击的内容
 */
@property(nonatomic, copy) void(^didSelectedCallback)(NSInteger index, NSString * content);
/// 数据源 数据, 下拉列表的内容数组.
@property(nonatomic, strong) NSArray * arrMDataSource;
// tableview以及cell的背景色, 如果不设置默认白色
@property(nonatomic, strong) UIColor * tabColor;
// 文字的颜色, 默认黑色
@property(nonatomic, strong) UIColor * txtColor;
@end

  • .m文件
  • 创建一个tableview加到view上
  • cell个数即为数据源的数组中数据条数.

//
//  FMenuAlert.m
//  get_loc_taped
//
//  Created by 裴波波 on 2018/1/5.
//  Copyright © 2018年 裴波波. All rights reserved.
//

#import "FMenuAlert.h"

@interface FMenuAlert ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) UITableView * tableView;
@end

@implementation FMenuAlert

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

-(instancetype)init{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}

-(void)setTabColor:(UIColor *)tabColor{
    _tabColor = tabColor;
    self.tableView.backgroundColor = tabColor;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self initUI];
    }
    return self;
}


-(void)initUI{

    UITableView * tableView = [UITableView new];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.frame = self.bounds;
    [self addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    tableView.rowHeight = 30;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"fmenualert"];
}

-(void)setArrMDataSource:(NSMutableArray *)arrMDataSource{
    _arrMDataSource = arrMDataSource;
    [_tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.didSelectedCallback) {
        self.didSelectedCallback(indexPath.row, _arrMDataSource[indexPath.row]);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrMDataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"fmenualert" forIndexPath:indexPath];
    cell.textLabel.text = _arrMDataSource[indexPath.row];
    cell.textLabel.textColor = _txtColor ? _txtColor : [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.contentView.backgroundColor = self.tabColor;
    cell.textLabel.backgroundColor = self.tabColor;
    cell.textLabel.font = _cusFont ? _cusFont : KFONT(15);
    return cell;
}

// 以下适配iOS11+
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

@end

  • 如何调用如下:
  • 创建并设置必要的属性
  • 实现回调
    FMenuAlert * alert =  [[FMenuAlert alloc] initWithFrame:KFRAME(tag*(KScreen_Width / 3.0), 85, KScreen_Width / 3.0, height)];
    self.alert = alert;
    alert.cusFont = KFONT(12);
    alert.tabColor = KWHITE_COLOR;
   [alert setDidSelectedCallback:^(NSInteger index, NSString *content) {
   //回调中要实现的功能.
    }];
原文地址:https://www.cnblogs.com/adampei-bobo/p/8582146.html