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) {
   //回調中要實現的功能.
    }];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM