快筛菜单

一个简单的快筛菜单

项目中列表页上方需要加一个快筛的视图,效果如图

效果图

模拟器中cell的横线不显示。

  • .h文件
//
//  SouFunEBRChooseMenuView.h
//  SouFun
//
//  Created by qinman on 16/3/23.
//
//

#import <UIKit/UIKit.h>

@protocol ChooseMenu <NSObject>

/**
 *  单击的代理方法。选择某一行
 *
 *  @param text  选择的文本值
 *  @param index 选择的行数
 */
- (void)onClick:(NSString *)text SelectedIndex:(NSInteger)index;
- (void)dismiss;
@end

@interface SouFunEBRChooseMenuView : UIView <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) id<ChooseMenu> delegate;
@property (nonatomic, assign) NSInteger selectedHightLightIndex;

/**
 *  初始化方法
 *
 *  @param frame
 *  @param dataArray 一维数组,存放筛选项的备选项
 */
- (instancetype)initWithFrame:(CGRect)frame withData:(NSArray *)dataArray;

@end

  • .m文件
//
//  SouFunEBRChooseMenuView.m
//  SouFun
//
//  Created by qinman on 16/3/23.
//
//

#import "SouFunEBRChooseMenuView.h"

#define CellHeight  44

@interface SouFunEBRChooseMenuView() <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) NSString *text;
@end

static NSString *cellID = @"cellID";

@implementation SouFunEBRChooseMenuView

- (UIView *)backgroundView{
    if (!_backgroundView) {
        _backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backgroundView.backgroundColor=[UIColor blackColor];
        _backgroundView.alpha=0.5f;
        [self addSubview: _backgroundView];
    }
    return _backgroundView;
}

- (instancetype)initWithFrame:(CGRect)frame withData:(NSArray *)dataArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = dataArray;
        [self createTableView];
    }
    return self;
}


- (void)createTableView {
    [self createBackgroundView];
    CGFloat height = self.dataArray.count * CellHeight;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView = tableView;
    [self addSubview:self.tableView];
}

#pragma mark - - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, CellHeight-0.5, KSCREEN_WIDTH, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:line];

        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    cell.textLabel.text = self.dataArray[indexPath.row];

    if (self.selectedHightLightIndex == indexPath.row) {
        cell.textLabel.textColor = [UIColor blueColor];
    }
    else {
        cell.textLabel.textColor = [UIColor blackColor];
    }

    return cell;
}

#pragma mark - - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.selectedHightLightIndex = indexPath.row;
    [self.tableView reloadData];
    self.text = cell.textLabel.text;
    if ([self.delegate respondsToSelector:@selector(onClick:SelectedIndex:)]) {
        [self.delegate onClick:self.text SelectedIndex:indexPath.row];
        [self remove:nil];
    }
}

#pragma mark - - 蒙版
-(void)createBackgroundView{

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove:)];
    [self.backgroundView addGestureRecognizer:tap];
    [self.backgroundView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
}

- (void)remove:(UITapGestureRecognizer *)sender {
    self.backgroundView.frame = CGRectZero;
    self.tableView.frame = CGRectZero;
    if ([self.delegate respondsToSelector:@selector(dismiss)]) {
        [self.delegate dismiss];
    }
}


@end

效果图中的三角是一张图片,就不贴代码了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值