iOS下拉列表选择
下拉展开列表选择
1.item类
//
// Item.h
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, ItemType){
ItemTypeExamination = 0,//招生季
ItemTypeTime = 1,//时间
ItemTypeStatues = 2,//状态
};
NS_ASSUME_NONNULL_BEGIN
@interface Item : UIButton
@property (nonatomic,assign)NSInteger index;//选中的角标
-(UIButton *)initItemWithTitle:(NSString *)title ItemTag:(NSInteger)itemTag frame:(CGRect)frame action:(SEL)action view:(UIView *)superview;
-(void)setTitle:(NSString *)title AndSelectIndex:(NSInteger)index;
@end
NS_ASSUME_NONNULL_END
//
// Item.m
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import “Item.h”
@implementation Item
-(UIButton *)initItemWithTitle:(NSString *)title ItemTag:(NSInteger)itemTag frame:(CGRect)frame action:(SEL)action view:(UIView *)superview{
Item *item = [Item buttonWithType:UIButtonTypeCustom];
[item setImage:[UIImage imageNamed:@“rightImage_state”] forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:@“rightImage_state”] forState:UIControlStateHighlighted];
[item setImage:[UIImage imageNamed:@“rightImage_state_normal”] forState:UIControlStateSelected];
[item setImage:[UIImage imageNamed:@“rightImage_state_normal”] forState:UIControlStateSelected | UIControlStateHighlighted];
[item setTitle:title forState:UIControlStateNormal];
[item.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[item setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[item setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[item setTitleColor:[UIColor redColor] forState:UIControlStateSelected | UIControlStateHighlighted];
[item setAdjustsImageWhenHighlighted:NO];
item.frame = frame;
// 设置左右排列
[item setTitleEdgeInsets:UIEdgeInsetsMake(0, -(item.imageView.bounds.size.width + 4), 0, item.imageView.bounds.size.width + 4)];
[item setImageEdgeInsets:UIEdgeInsetsMake(0, item.titleLabel.bounds.size.width, 0, -item.titleLabel.bounds.size.width)];
item.tag = itemTag;
item.index = 0;//默认下标是0
[item addTarget:superview action:action forControlEvents:UIControlEventTouchUpInside];
return item;
}
-(void)setTitle:(NSString *)title AndSelectIndex:(NSInteger)index{
[self setTitle:title forState:UIControlStateNormal];
self.index = index;
// 设置左右排列
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(self.imageView.bounds.size.width + 4), 0, self.imageView.bounds.size.width + 4)];
[self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width, 0, -self.titleLabel.bounds.size.width)];
}
@end
2.view类
//
// ScreeningView.h
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import <UIKit/UIKit.h>
#import “Item.h”
// 屏幕宽
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
// 屏幕高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@protocol ScreeningDelegate
///展开某标题下的视图 并传入当前选中的下标
-(void)showScreeningViewWithItemTag:(NSInteger)itemTag selectIndex:(NSInteger)selectIndex;
///收起试图
-(void)closeScreeningView;
@end
NS_ASSUME_NONNULL_BEGIN
@interface ScreeningView : UIView
@property (nonatomic,strong)NSMutableArray *itemsArray;
//数据源
//根据数据源 来显示标题可自行处理 这里使用的格式是@[@[NSString,NSString,nil],@[NSString,NSString,nil]@[NSString,NSString,nil]]
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,weak) id delegate;
@end
NS_ASSUME_NONNULL_END
//
// ScreeningView.m
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import “ScreeningView.h”
@interface ScreeningView(){
CGFloat __Height;//视图高度 用以设置item 高度
}
@end
@implementation ScreeningView
(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self){
__Height = frame.size.height;
}
return self;
}
//初始化试图内容
-(void)configData{
for (int index = 0 ; index < self.dataArray.count ; index++ ) {
NSArray *array = self.dataArray[index];
Item *item = [[Item alloc]initItemWithTitle:[array firstObject] ItemTag:index frame:CGRectMake(SCREEN_WIDTH/self.dataArray.count*index, 0, SCREEN_WIDTH/self.dataArray.count, __Height) action:@selector(itemButtonClick:) view:self];
[self addSubview:item];
[self.itemsArray addObject:item];//item数组
}
}
//item 点击事件
-(void)itemButtonClick:(Item *)sender{
sender.selected = !sender.selected;
if (sender.selected) {
//1.展开列表
if (_delegate && [_delegate respondsToSelector:@selector(showScreeningViewWithItemTag:selectIndex:)]) {
[_delegate showScreeningViewWithItemTag:sender.tag selectIndex:sender.index];
}else{
NSLog(@“请设置代理并实现方法用以展开列表”);
}
//2.重置其他按钮状态
for (Item *item in self.itemsArray) {
item.selected = (item.tag == sender.tag);
}
}else{//收起列表
if (_delegate && [_delegate respondsToSelector:@selector(closeScreeningView)]) {
[_delegate closeScreeningView];
}else{
NSLog(@“请设置代理并实现方法用以关闭列表”);
}
}
}
-(NSMutableArray *)itemsArray{
if (!_itemsArray) {
_itemsArray = [[NSMutableArray alloc]init];
}
return _itemsArray;
}
-(void)setDataArray:(NSMutableArray *)dataArray{
_dataArray = dataArray;
[self configData];
}
@end
3.列表控制器
//
// ScreeningListController.h
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^didCellBlock)(NSInteger type,NSInteger index);
@interface ScreeningListController : UIViewController
@property (nonatomic,assign)NSInteger tag;//第几个item
@property (nonatomic,assign)NSInteger selectIndex;//此item对应的下拉列表中哪条被选中
@property (nonatomic,strong)NSMutableArray *dataArray;
-(void)showlistTag:(NSInteger)tag selectIndex:(NSInteger )selectIndex dataArray:(NSMutableArray *)dataArray;
@property (nonatomic,copy)didCellBlock block;//选中了某一条下拉数据
@end
NS_ASSUME_NONNULL_END
//
// ScreeningListController.m
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import “ScreeningListController.h”
#define ScreeningstatusHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define ScreeningnavHeight 44
#define ScreeningbarHeight (ScreeningstatusHeight + ScreeningnavHeight)
@interface ScreeningListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableview;
@end
@implementation ScreeningListController
(void)viewDidLoad {
[super viewDidLoad];
self.view.frame = CGRectMake(0, ScreeningbarHeight+40, ([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height)-ScreeningbarHeight-40);
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.view addSubview:self.tableview];
// Do any additional setup after loading the view.
}
//更新数据源
-(void)showlistTag:(NSInteger)tag selectIndex:(NSInteger )selectIndex dataArray:(NSMutableArray *)dataArray{
self.tag = tag;
self.selectIndex = selectIndex;
self.dataArray = dataArray[tag];
[self.tableview reloadData];
//y添加一个展开动画
self.tableview.frame = CGRectMake(0, 0, ([UIScreen mainScreen].bounds.size.width), 0);
[UIView animateWithDuration:0.5 animations:^{
self.tableview.frame = CGRectMake(0, 0, ([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height)-ScreeningbarHeight-40);
}];
}
#pragma mark *************UITableViewDelegate UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = self.dataArray[indexPath.row];
if (indexPath.row == self.selectIndex) {
cell.textLabel.textColor = [UIColor redColor];
}else{
cell.textLabel.textColor = [UIColor blackColor];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectIndex = indexPath.row;
[self.tableview reloadData];
if (self.block) {
self.block(self.tag, self.selectIndex);
}
}
#pragma mark ---- 懒加载控制器控件
-(UITableView *)tableview{
if (!_tableview) {
_tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ([UIScreen mainScreen].bounds.size.width), ([UIScreen mainScreen].bounds.size.height)-ScreeningbarHeight-40) style:UITableViewStylePlain];
_tableview.backgroundColor = [UIColor clearColor];
_tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableview.delegate = self;
_tableview.dataSource = self;
_tableview.estimatedRowHeight = 0;
_tableview.bounces = NO;
_tableview.estimatedSectionHeaderHeight = 0;
_tableview.estimatedSectionFooterHeight = 0;
//
_tableview.rowHeight = UITableViewAutomaticDimension;
_tableview.tableFooterView = [UIView new];
_tableview.tableHeaderView = [UIView new];
if (@available(iOS 11.0, *)) {
_tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
return _tableview;
}
@end
使用
//
// ViewController.m
// DemoForTest
//
// Created by 刘永吉 on 2019/9/9.
// Copyright © 2019 lyj. All rights reserved.
//
#import “ViewController.h”
#import “ScreeningView.h”
#import “ScreeningListController.h”
#define ScreeningstatusHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define ScreeningnavHeight 44
#define ScreeningbarHeight (ScreeningstatusHeight + ScreeningnavHeight)
@interface ViewController ()
@property (nonatomic,strong)ScreeningView *screeningview;//筛选视图
@property (nonatomic,strong)ScreeningListController *screeningvc;//下拉显示的控制器
///test data 三组数据源
@property (nonatomic,strong)NSArray *dataArray1;
@property (nonatomic,strong)NSArray *dataArray2;
@property (nonatomic,strong)NSArray *dataArray3;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @“DEMO”;
self.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.screeningview];
self.screeningview.dataArray = [[NSMutableArray alloc]initWithArray:@[self.dataArray1,self.dataArray2,self.dataArray3]];
//添加下拉展示控制器
[self addChildViewController:self.screeningvc];
[self.view addSubview:self.screeningvc.view];
self.screeningvc.view.hidden = YES;
}
-(void)showScreeningViewWithItemTag:(NSInteger)itemTag selectIndex:(NSInteger)selectIndex{
self.screeningvc.view.hidden = NO;
self.screeningvc.view.alpha = 1;
[self.screeningvc showlistTag:itemTag selectIndex:selectIndex dataArray:[[NSMutableArray alloc]initWithArray:@[self.dataArray1,self.dataArray2,self.dataArray3]]];
__weak typeof(self) weakSelf = self;
self.screeningvc.block = ^(NSInteger tag, NSInteger index) {
[weakSelf setScreeningViewItemWithTag:tag index:index];
};
}
-(void)setScreeningViewItemWithTag:(NSInteger)tag index:(NSInteger)index{
[self closeScreeningView];
Item *item = [self.screeningview.itemsArray objectAtIndex:tag];
NSArray *array = @[self.dataArray1,self.dataArray2,self.dataArray3];
[item setTitle:[[array objectAtIndex:tag] objectAtIndex:index] AndSelectIndex:index];
}