快筛菜单
2016-05-27 11:17
309 查看
一个简单的快筛菜单
项目中列表页上方需要加一个快筛的视图,效果如图模拟器中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
效果图中的三角是一张图片,就不贴代码了。
相关文章推荐
- swift库
- Redis概述
- Python 函数 切片 迭代 列表生成器
- 阿里音乐流行趋势预测大赛一起做-(5)温故知新
- Lombok 安装、入门
- 统计一周系统负荷、内存利用率和磁盘利用率 平均值和峰值
- 谈谈iOS app的线上性能监测
- iOS webView 检测当前网页的请求错误码400、404等错误码
- 博客虚拟主机的购买技巧
- 分治法——二分查找
- POJ 1502 MPI Maelstrom最短路问题
- 有关window.location.hash
- 遍历Map的四种方法
- AFNetworking 3.0迁移指南
- Android 最火的快速开发框架AndroidAnnotations使用详解
- date java 时间相关
- tomcat6目录结构
- 初识Retrofit
- 34.Search for a Range LeetCode Java版代码
- 软件设计模式详解:OCP原则