先上图
页面很简单,5个条件下都是单选。就不要写的太复杂了。
两个循环即可以实现 。 展示和点完成的请求参数在一起实现。
外循环:展示5个条件
内循环: 展示每个条件下的选项
每个条件的view
EachView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface EachView : UIView
+ (EachView *)makeEachViewWithTitle:(NSString *)title chList:(NSArray *)list;
- (NSDictionary*)getPostDic;
- (void)makeDefault;
@end
NS_ASSUME_NONNULL_END
EachView.m
#import "EachView.h"
#import "ViewController.h"
@implementation EachView
+ (EachView *)makeEachViewWithTitle:(NSString *)title chList:(NSArray *)list;{
EachView *view = [[EachView alloc]init];
view.width = SCREEN_WIDTH;
//
//左上title
//
NSString *titleS = title;
MyLabel *left = [[MyLabel alloc]initWithFrame:CGRectZero bgColor:[UIColor clearColor] title:titleS titleCollor:[UIColor grayColor] fontSize:13 * SCALE_FONT cornerRadius:0];
[left sizeToFit];
left.top = 15;
left.left = 20;
[view addSubview:left];
NSArray *btnNames = list;
for(int i = 0 ; i < btnNames.count; i ++){
//
//下方按钮
//
UIButton *amtButton = [[UIButton alloc]init];
[amtButton setTitle:[NSString stringWithFormat:@"%@%@",btnNames[i],@""] forState:UIControlStateNormal];
amtButton.titleLabel.font = [UIFont systemFontOfSize:15];
amtButton.titleLabel.numberOfLines = 0;
amtButton.tag = 100 + i;
if (i == 0) {
[view setSelectedStyle:amtButton];
}else{
[view setdeSelectedStyle:amtButton];
}
//
//除去两边空白的平均宽度
//
float width = (SCREEN_WIDTH - 10 * 2) / 3.0 ;
amtButton.frame = CGRectMake(10 + width * (i % 3) ,
(left.bottom + 5 + 1 * HEIGHT_SCALE + (i / 3) * 40 * HEIGHT_SCALE) ,
width - 10,
30);
__block UIButton *weakAmtButton = amtButton;
[amtButton setTapActionWithBlock:^{
//
//设置选中与不选中
//
[view setSelectedStyle:weakAmtButton];
for (UIButton *but in view.subviews) {
if ((but.tag > 99) && (but != amtButton)) {
[view setdeSelectedStyle:but];
}
}
}];
[view addSubview:amtButton];
view.height = amtButton.bottom;
}
return view;
}
//
//默认样式
//
- (void)makeDefault{
for (UIButton *but in self.subviews) {
if ([but isKindOfClass:[UIButton class]]) {
if ( but.tag == 100) {
[self setSelectedStyle:but];
}else{
[self setdeSelectedStyle:but];
}
}
}
}
//
//选中样式
//
- (void)setSelectedStyle:(UIButton *)amtButton{
amtButton.selected = YES;
[amtButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
amtButton.layer.borderColor = [UIColor blueColor].CGColor;
[amtButton setBackgroundColor:[UIColor blueColor]];
amtButton.layer.borderWidth = 0.5;
amtButton.layer.cornerRadius = 2;
}
//
//未选中样式
//
- (void)setdeSelectedStyle:(UIButton *)amtButton{
amtButton.selected = NO;;
amtButton.layer.borderWidth = 0.5;
amtButton.layer.cornerRadius = 2;
[amtButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
amtButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
[amtButton setBackgroundColor:[UIColor lightGrayColor]];
}
//
//获取请求参数 每个btn对应不同的参数
//
- (NSDictionary*)getPostDic{
NSDictionary * _lastDictionary = @{@"全部" :@"0",
@"活期类" :@"huoqi",
@"定期类" :@"dingqi",
@"结构性产品" :@"jiegou",
@"私人银行" :@"siren",
@"全部" :@"0",
@"1-30天" :@"1",
@"31-60天" :@"2",
@"61-90天" :@"3",
@"91-180天" :@"4",
@"181-365天" :@"5",
@"365天+" :@"12",
@"全部" :@"0",
@"5万以下" :@"4",
@"5-600万" :@"5",
@"10-600万" :@"10",
@"600万+" :@"600",
@"全部":@"0",
@"保本":@"bao",
@"其他":@"other",
@"全部" :@"0",
@"人民币" :@"rmb",
@"美元" :@"my",
@"英镑" :@"yb",
@"港币" :@"gb",
@"澳元" :@"ay",
};
//
//寻找当前选中的按钮
//
NSString *title = @"";
for (UIButton *btn in self.subviews) {
if ([btn isKindOfClass:[UIButton class]] && btn.selected == YES) {
title = btn.titleLabel.text;
}
}
NSDictionary *thisDic = nil;
@try{
//
//当前选中的条件
//
if (self.tag == 0) {
thisDic = @{@"type":_lastDictionary[title]};
}else if (self.tag == 1){
thisDic = @{@"date":_lastDictionary[title]};
}else if (self.tag == 2){
thisDic = @{@"start":_lastDictionary[title]};
}else if (self.tag == 3){
thisDic = @{@"baobenxingzhi":_lastDictionary[title]};
}else if (self.tag == 4){
thisDic = @{@"currency":_lastDictionary[title]};
}
}
@catch(NSException *ex){
NSLog(@"崩溃了");
}
@finally{
return thisDic;
}
}
@end
外层的view
ChooseView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ChooseView : UIView
+ (ChooseView *)makeChooseViewByScreeningViewWithTop:(double)top;
@end
NS_ASSUME_NONNULL_END
ChooseView.m
#import "ChooseView.h"
#import "ViewController.h"
#import "EachView.h"
@implementation ChooseView
+ (ChooseView *)makeChooseViewByScreeningViewWithTop:(double)top;{
ChooseView *chooseView = [[ChooseView alloc]init];
chooseView.backgroundColor = [UIColor whiteColor];
chooseView.width = SCREEN_WIDTH;
chooseView.height = SCREEN_HEIGHT_NEW - top;
UIScrollView *scrollow = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, chooseView.height - 40)];
[chooseView addSubview:scrollow];
scrollow.backgroundColor = [UIColor clearColor];
double topX = 0 ;
for(int i = 0; i < 5 ; i ++){
NSArray *_listArray = @[@"产品类型",@"产品期限",@"起购金额",@"保本性质",@"币种"];
NSArray *chList = nil;
if (i == 0) {
chList = @[@"全部",@"活期类",@"定期类",@"结构性产品",@"私人银行"];
}else if (i == 1){
chList = @[@"全部",@"1-30天",@"31-60天",@"61-90天",@"91-180天",@"181-365天",@"365天+"];
}else if (i == 2){
chList = @[@"全部",@"5万以下",@"5-600万",@"10-600万",@"600万+", ];
}else if (i == 3){
chList = @[@"全部",@"保本",@"其他", ];
}else if (i == 4){
chList = @[@"全部",@"人民币",@"美元",@"英镑",@"港币",@"澳元"];
}
EachView *eachView = [EachView makeEachViewWithTitle:_listArray[i] chList:chList ];
eachView.top = topX;
eachView.tag = i;
[scrollow addSubview:eachView];
eachView.backgroundColor = [UIColor whiteColor];
if (i==4) {
eachView.height += 50;
}
topX = eachView.bottom;
}
scrollow.contentSize = CGSizeMake(SCREEN_WIDTH, topX + 50);
double thisHeight = 50.0;
//下面按钮
UIView *resetBtn = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, thisHeight- 1)];
resetBtn.backgroundColor = [UIColor whiteColor];
[chooseView addSubview:resetBtn];
resetBtn.bottom = chooseView.height;
scrollow.contentSize = CGSizeMake(SCREEN_WIDTH, topX + thisHeight);
//
//重置
//
UIButton *cleanAllButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2.0, thisHeight)];
[resetBtn addSubview:cleanAllButton];
[cleanAllButton setTitle:@"重置" forState:UIControlStateNormal];
[cleanAllButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cleanAllButton setBackgroundImage:[UIImage imageNamed:@"White.png"] forState:UIControlStateNormal];
[cleanAllButton setTapActionWithBlock:^{
for (int i = 0; i < scrollow.subviews.count; i ++) {
EachView *seachView = scrollow.subviews[i];
if ([seachView isKindOfClass:[EachView class]]) {
[seachView makeDefault];
}
}
}];
//
//完成
//
UIButton *sureButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0, 0, SCREEN_WIDTH/2.0, thisHeight)];
[resetBtn addSubview:sureButton];
[sureButton setTitle:@"完成" forState:UIControlStateNormal];
[sureButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
resetBtn.userInteractionEnabled = YES;
[sureButton setTapActionWithBlock:^{
NSMutableDictionary *allParamsDic = [[NSMutableDictionary alloc]initWithCapacity:5];
for (int i = 0; i < scrollow.subviews.count; i ++) {
EachView *seachView = scrollow.subviews[i];
if ([seachView isKindOfClass:[EachView class]]) {
NSDictionary *dic = [seachView getPostDic];
[allParamsDic addEntriesFromDictionary:dic];
}
}
//
//发接口
//
NSLog(@"参数%@",allParamsDic);
}];
return chooseView;
}
@end
效果图
点击完成 可打印选中的参数
工程下载地址 https://github.com/XiaoHeHe1/FilterListView