工作中用到一个右侧弹出的筛选框,自己写了一个,有2个代理方法回调确认和取消,记录一下,如果后面要用就直接拿来用
.h 文件
#import
@protocol categorizeSiftDelegate;
@interface categorizeSiftView : UIView
@property(nonatomic ,weak)id<categorizeSiftDelegate> delegate;
-(void)show;
-(void)dismiss;
@end
@protocol categorizeSiftDelegate <NSObject>
-(void)categorizeSiftViewDidClickOkButtonWithResult:(NSDictionary *)result;
-(void)categorizeSiftViewDidClickCancleButton;
@end
.m文件
typedef enum : NSUInteger {
MAIN_DATASOURCE,
CITY_DATASOURCE,
SALARY_DATASOURCE,
WORKEXP_DATASOURCE,
DEGREE_DATASOURCE,
WORKTYPE_DATASOURCE,
PUBLISHTIME_DATASOURCE
} TABLE_DATASOURCE;
#import "categorizeSiftView.h"
@interface categorizeSiftView ()<UITableViewDataSource,UITableViewDelegate>
{
NSArray *hotCityArr;
NSArray *normalCityArr;
NSDictionary *normalCityDict;
NSArray *keyArr;
NSArray *salaryScopes;
NSArray *workexps;
NSArray *degrees;
NSArray *workTypes;
NSArray *publishTimes;
}
@property(nonatomic ,retain)UITableView *tableView;
@property(nonatomic ,retain)UIView *view;
@property(nonatomic ,retain)UILabel *titleLabel;
@property(nonatomic ,assign)int datasourceType;
@property(nonatomic ,copy)NSString *citySelect;
@property(nonatomic ,copy)NSString *salarySelect;
@property(nonatomic ,copy)NSString *workexpSelect;
@property(nonatomic ,copy)NSString *degreeSelect;
@property(nonatomic ,copy)NSString *workTypeSelect;
@property(nonatomic ,copy)NSString *publishTimeSelect;
@property(nonatomic ,retain)UIButton *okBtn;
@property(nonatomic ,retain)UIButton *cancleBtn;
@end
@implementation categorizeSiftView
//创建基本图形
- (instancetype)init
{
self = [super initWithFrame:[UIScreen mainScreen].bounds];
if (self)
{
self.view = [[UIView alloc]initWithFrame:CGRectMake(kScreenwidth, 0, kScreenwidth-100, kScreenheight)];
self.view.backgroundColor = [UIColor colorWithRed:0.12 green:0.61 blue:0.46 alpha:1];
[self addSubview:self.view];
self.datasourceType = MAIN_DATASOURCE;
[self initDataSources];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStyleGrouped];
self.tableView.sectionFooterHeight = 0.0f;
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
[self.view addSubview:self.tableView];
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
self.okBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 25, 50, 30)];
[self.okBtn setTitle:@"确定" forState:UIControlStateNormal];
self.okBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[self.okBtn addTarget:self action:@selector(ClickOkBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.okBtn];
self.cancleBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width-55, 25, 50, 30)];
[self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
self.cancleBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[self.cancleBtn addTarget:self action:@selector(ClickCancleBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.cancleBtn];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 25, self.view.bounds.size.width-110, 30)];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.text = @"筛选条件";
[self.view addSubview:self.titleLabel];
}
return self;
}
//模型层的建立
-(void)initDataSources
{
hotCityArr = @[@"北京",@"上海",@"广州",@"深圳",@"成都",@"杭州"];
normalCityArr = @[@"北京",@"长春",@"成都",@"重庆",@"长沙",@"常州",@"东莞",@"大连",@"东营",@"福州",@"佛山",@"贵阳",@"广州",@"哈尔滨",@"合肥",@"呼和浩特",@"海口",@"杭州",@"惠州",@"金华",@"吉林",@"济南",@"济宁",@"嘉兴",@"昆明",@"昆山",@"廊坊",@"丽水",@"兰州",@"宁波",@"南昌",@"南京",@"南宁",@"青岛",@"泉州",@"上海",@"石家庄",@"汕头",@"绍兴",@"沈阳",@"深圳",@"苏州",@"泰安",@"天津",@"太原",@"台州",@"潍坊",@"武汉",@"乌鲁木齐",@"无锡",@"温州",@"西安",@"香港",@"厦门",@"徐州",@"银川",@"扬州",@"烟台",@"淄博",@"珠海",@"镇江",@"肇庆",@"中山",@"郑州"];
normalCityDict = @{@"B":@[@"北京"],@"C":@[@"长春",@"成都",@"重庆",@"长沙",@"常州"],@"D":@[@"东莞",@"大连",@"东营"],@"F":@[@"福州",@"佛山"],@"G":@[@"贵阳",@"广州"],@"H":@[@"哈尔滨",@"合肥",@"呼和浩特",@"海口",@"杭州",@"惠州"],@"J":@[@"金华",@"吉林",@"济南",@"济宁",@"嘉兴"],@"K":@[@"昆明",@"昆山"],@"L":@[@"廊坊",@"丽水",@"兰州"],@"N":@[@"宁波",@"南昌",@"南京",@"南宁"],@"Q":@[@"青岛",@"泉州"],@"S":@[@"上海",@"石家庄",@"汕头",@"绍兴",@"沈阳",@"深圳",@"苏州"],@"T":@[@"泰安",@"天津",@"太原",@"台州"],@"W":@[@"潍坊",@"武汉",@"乌鲁木齐",@"无锡",@"温州"],@"X":@[@"西安",@"香港",@"厦门",@"徐州"],@"Y":@[@"银川",@"扬州",@"烟台"],@"Z":@[@"淄博",@"珠海",@"镇江",@"肇庆",@"中山",@"郑州"]};
keyArr = [normalCityDict.allKeys sortedArrayUsingSelector:@selector(compare:)];
salaryScopes = @[@"低于2K",@"2K~5K",@"5K~10K",@"10K~15K",@"15K~25K",@"25K~50K",@"大于50K"];
workexps = @[@"应届毕业生",@"1年以上",@"1~3年",@"3~5年",@"5~8年",@"8~10年",@"10年以上"];
degrees = @[@"大专",@"本科",@"硕士",@"博士",@"其他"];
workTypes = @[@"全职",@"兼职",@"实习"];
publishTimes = @[@"1天之内",@"3天之内",@"1周之内",@"1个月之内",@"1个月以前"];
}
#pragma mark - BtnClick
-(void)ClickCancleBtn
{
if (self.datasourceType != MAIN_DATASOURCE)
{
self.datasourceType = MAIN_DATASOURCE;
[self showBtn];
[self.tableView reloadData];
return;
}
[self.delegate categorizeSiftViewDidClickCancleButton];
[self dismiss];
}
-(void)ClickOkBtn
{
[self.delegate categorizeSiftViewDidClickOkButtonWithResult:
@{@"city":self.citySelect?self.citySelect:@"",
@"salary":self.salarySelect?self.salarySelect:@"",
@"workexp":self.workexpSelect?self.workexpSelect:@"",
@"degree":self.degreeSelect?self.degreeSelect:@"",
@"workType":self.workTypeSelect?self.workTypeSelect:@"",
@"publishTime":self.publishTimeSelect?self.publishTimeSelect:@""}];
[self dismiss];
}
#pragma mark - UITableViewDataSource & delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//根据数据类型区分
if (self.datasourceType == CITY_DATASOURCE)
{
return normalCityDict.count +2;
}
else if(self.datasourceType != MAIN_DATASOURCE)
{
return 1;
}
else
{
return 2;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//根据数据类型区分
if (self.datasourceType == CITY_DATASOURCE)
{
if (section == 0)
{
return hotCityArr.count;
}
else if (section == 1)
{
return 1;
}
else
{
NSString *key = keyArr[section-2];
NSArray *nameSection = normalCityDict[key];
return [nameSection count];
}
}
else if (self.datasourceType != MAIN_DATASOURCE)
{
switch (self.datasourceType)
{
case SALARY_DATASOURCE:
return salaryScopes.count;
break;
case WORKEXP_DATASOURCE:
return workexps.count;
break;
case DEGREE_DATASOURCE:
return degrees.count;
break;
case WORKTYPE_DATASOURCE:
return workTypes.count;
break;
default:
return publishTimes.count;
break;
}
}
else
{
if (section==0)
{
return 1;
}
else
{
return 5;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据数据类型区分
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
if (self.datasourceType == CITY_DATASOURCE)
{
if (indexPath.section == 0)
{
cell.textLabel.text = [hotCityArr objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:self.citySelect])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
else if (indexPath.section == 1)
{
cell.textLabel.text = @" ";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.textLabel.text = [[normalCityDict valueForKey:[keyArr objectAtIndex:indexPath.section-2]]objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:self.citySelect])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
else if (self.datasourceType != MAIN_DATASOURCE)
{
switch (self.datasourceType)
{
case SALARY_DATASOURCE:
cell.textLabel.text = salaryScopes[indexPath.row];
break;
case WORKEXP_DATASOURCE:
cell.textLabel.text = workexps[indexPath.row];
break;
case DEGREE_DATASOURCE:
cell.textLabel.text = degrees[indexPath.row];
break;
case WORKTYPE_DATASOURCE:
cell.textLabel.text = workTypes[indexPath.row];
break;
default:
cell.textLabel.text = publishTimes[indexPath.row];
break;
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.section==0)
{
cell.textLabel.text = @"工作地点";
cell.detailTextLabel.text = self.citySelect;
}
else
{
switch (indexPath.row)
{
case 0:
cell.textLabel.text = @"月薪范围";
cell.detailTextLabel.text = self.salarySelect;
break;
case 1:
cell.textLabel.text = @"工作经验";
cell.detailTextLabel.text = self.workexpSelect;
break;
case 2:
cell.textLabel.text = @"最低学历";
cell.detailTextLabel.text = self.degreeSelect;
break;
case 3:
cell.textLabel.text = @"工作性质";
cell.detailTextLabel.text = self.workTypeSelect;
break;
default:
cell.textLabel.text = @"发布时间";
cell.detailTextLabel.text = self.publishTimeSelect;
break;
}
}
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//根据数据类型区分
if (self.datasourceType == CITY_DATASOURCE)
{
if (section == 0 || section == 1)
{
return 44;
}
else
{
return 22;
}
}
else
{
return 15;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据数据类型区分
if (self.datasourceType == CITY_DATASOURCE)
{
if (indexPath.section==1)
{
return 3;
}
else
return 44;
}
else
{
return 44;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//根据数据类型区分
if (self.datasourceType == CITY_DATASOURCE)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.citySelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
else if (self.datasourceType != MAIN_DATASOURCE)
{
switch (self.datasourceType)
{
case SALARY_DATASOURCE:
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.salarySelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
break;
case WORKEXP_DATASOURCE:
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.workexpSelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
break;
case DEGREE_DATASOURCE:
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.degreeSelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
break;
case WORKTYPE_DATASOURCE:
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.workTypeSelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
break;
default:
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.publishTimeSelect = cell.textLabel.text;
self.datasourceType = MAIN_DATASOURCE;
[self.tableView reloadData];
[self showBtn];
}
break;
}
}
else
{
if (self.datasourceType == MAIN_DATASOURCE)
{
if (indexPath.section==0)
{
self.datasourceType = CITY_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
}
else
{
switch (indexPath.row)
{
case 0:
self.datasourceType = SALARY_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
break;
case 1:
self.datasourceType = WORKEXP_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
break;
case 2:
self.datasourceType = DEGREE_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
break;
case 3:
self.datasourceType = WORKTYPE_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
break;
default:
self.datasourceType = PUBLISHTIME_DATASOURCE;
[self.tableView reloadData];
[self hiddenBtn];
break;
}
}
}
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.datasourceType == CITY_DATASOURCE)
{
if (section == 0)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 44)];
label.text = @" 热门城市";
label.backgroundColor = [UIColor colorWithWhite:0.94 alpha:1];
return label;
}
else if (section == 1)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 44)];
label.text = @" 全国城市";
label.backgroundColor = [UIColor colorWithWhite:0.94 alpha:1];
return label;
}
else
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 22)];
label.text = [NSString stringWithFormat:@" %@",[keyArr objectAtIndex:section-2]];
label.backgroundColor = [UIColor colorWithWhite:0.94 alpha:1];
label.font = [UIFont systemFontOfSize:15];
return label;
}
}
return nil;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.datasourceType == CITY_DATASOURCE)
{
return keyArr;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
{
if (self.datasourceType == CITY_DATASOURCE)
{
[tableView
scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index+2]
atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index +2;
}
return -1;
}
-(void)hiddenBtn
{
self.okBtn.hidden = YES;
switch (self.datasourceType)
{
case CITY_DATASOURCE:
self.titleLabel.text = @"选择工作城市";
break;
case SALARY_DATASOURCE:
self.titleLabel.text = @"选择薪酬范围";
break;
case WORKEXP_DATASOURCE:
self.titleLabel.text = @"选择工作经验";
break;
case DEGREE_DATASOURCE:
self.titleLabel.text = @"选择最低学历";
break;
case WORKTYPE_DATASOURCE:
self.titleLabel.text = @"选择工作性质";
break;
case PUBLISHTIME_DATASOURCE:
self.titleLabel.text = @"选择发布时间";
break;
default:
break;
}
}
-(void)showBtn
{
self.okBtn.hidden = NO;
self.titleLabel.text = @"筛选条件";
}
//监控滑动位移,控制view的移动
static float currentX;
static float startX;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//监控到点击,保存起始点位置
currentX = point.x;
startX = point.x;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//判断起始点位置是否与tableview重叠,如重叠不滑动
if (startX>100)
{
return;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
float moveX = self.view.frame.origin.x+(point.x-currentX);
[self.view setFrame:CGRectMake(moveX<100?100:moveX, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
currentX = point.x;
//滑动位移大于150直接隐藏
if (currentX-startX>=150)
{
[self dismiss];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//判断起始点位置是否与tableview重叠,如重叠不滑动
if (startX>100)
{
return;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (point.x-startX<150)
{
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:CGRectMake(100, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
//滑动结束后重置点位置
startX = 0;
currentX = 0;
}
//显示
-(void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:CGRectMake(100, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
//消失
-(void)dismiss
{
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:CGRectMake(kScreenwidth, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
@end