一个简单的快筛菜单
项目中列表页上方需要加一个快筛的视图,效果如图
模拟器中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
效果图中的三角是一张图片,就不贴代码了。