封装的带有筛选功能的选择器


封装的选择人员的一个View

1.SelectSellerView.h

#import <UIKit/UIKit.h>

#import "AllSellerModel.h"

@class SelectSellerView;

@protocol SelectSellerDelegate <NSObject>

-(void)selectData:(AllSellerModel *)model;


@end


@interface SelectSellerView : UIView


@property (nonatomic,weak)id<SelectSellerDelegate>sellerDelegate;

-(void)initDataSource:(NSArray *)arr;


@property (nonatomic,strong)UIViewController *superController;

2.SelectSellerView.m

@interface SelectSellerView()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

{

    NSMutableArray *dataSource;

    

    //筛选

    NSArray *AllpersonArr;

    UIButton *backgroundBtn;

    UIView *selectView;

    UILabel *selectLab;

    NSString *selectPersonStr;

    NSIndexPath *selectIndexPath;

}

@end


@implementation SelectSellerView

-(void)initDataSource:(NSArray *)arr{

    

    dataSource = [NSMutableArray arrayWithArray:arr];

    AllpersonArr = [NSArray arrayWithArray:arr];

    [self onClickSelect];

}

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    return self;

}


#pragma mark - UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return dataSource.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 0.1;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 0.1;

}

#pragma mark - UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellId = @"personCell";

    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellId];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    }

    for (UIView *view in cell.contentView.subviews) {

        [view removeFromSuperview];

    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    AllSellerModel *model = dataSource[indexPath.row];

    cell.textLabel.text = model.SellerName;

    return cell;

    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITextField *text = [backgroundBtn viewWithTag:100];

    AllSellerModel *model = dataSource[indexPath.row];

    text.text = model.SellerName;

    selectPersonStr = model.SellerName;

    selectLab.text = [NSString stringWithFormat:@"已选择:%@",selectPersonStr];

    UITableView *personTableView = [backgroundBtn viewWithTag:2];

    [personTableView removeFromSuperview];

    

}

#pragma mark - 弹出选择框

- (void)onClickSelect{

    

    backgroundBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    backgroundBtn.backgroundColor = [UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:0.8];

    [backgroundBtn addTarget:self action:@selector(onClickCancel) forControlEvents:UIControlEventTouchUpInside];

    

    [self addSubview:backgroundBtn];

    [backgroundBtn  bringSubviewToFront:self];

    

    

    selectView  = [[UIView alloc] initWithFrame:CGRectMake(40, 160, WIDTH-80, 130)];

    selectView.layer.cornerRadius = 8;

    selectView.backgroundColor = [UIColor whiteColor];

    selectView.layer.borderWidth = 1;

    selectView.layer.borderColor = background.CGColor;

    

    

    

    

    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, WIDTH-100, 30)];

    tf.tag = 100;

    tf.delegate = self;

    [tf addTarget:self action:@selector(textFieldChang:) forControlEvents:UIControlEventEditingChanged];

    tf.font = [UIFont systemFontOfSize:13.0];

    //    [tf becomeFirstResponder];

    tf.clearButtonMode = UITextFieldViewModeWhileEditing;

    tf.layer.cornerRadius = 4;

    tf.layer.borderColor = background.CGColor;

    tf.layer.borderWidth = 1;

    tf.placeholder = @"请输入";

    [selectView addSubview:tf];

    

    

    

    selectLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, WIDTH-100, 30)];

    selectLab.textColor = [UIColor grayColor];

    selectLab.font = [UIFont systemFontOfSize:15.0];

    selectLab.text = [NSString stringWithFormat:@"已选择:%@",selectPersonStr];

    [selectView addSubview:selectLab];

    

    NSArray *arr = [[NSArray alloc] initWithObjects:@"取消",@"确定" ,nil];

    for (int i = 0; i < 2; i++) {

        UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH-80)/2*i, 90, (WIDTH-80)/2, 40)];

        [cancel setTitle:arr[i] forState:UIControlStateNormal];

        [cancel setTitleColor:TextblueColor forState:UIControlStateNormal];

        [selectView addSubview:cancel];

        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cancel.frame.size.width, 1)];

        line.backgroundColor = background;

        [cancel addSubview:line];

        if (i == 0) {

            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(cancel.frame.size.width-1, 0,1, 40)];

            line.backgroundColor = background;

            [cancel addSubview:line];

            [cancel addTarget:self action:@selector(onClickCancel) forControlEvents:UIControlEventTouchUpInside];

        }else{

            

            [cancel addTarget:self action:@selector(onClickConform) forControlEvents:UIControlEventTouchUpInside];

        }

    }

    [backgroundBtn addSubview:selectView];

    

    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(40, 215, WIDTH-80, 300)];

    tableview.delegate = self;

    tableview.dataSource = self;

    tableview.bounces = NO;

    tableview.backgroundColor = [UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:0.1];

    dataSource = [NSMutableArray arrayWithArray:AllpersonArr];

    [backgroundBtn addSubview:tableview];

    [tableview bringSubviewToFront:backgroundBtn];

}

- (void)onClickCancel{

    

    [self removeFromSuperview];

}

- (void)onClickConform{

    

    if (dataSource.count == 0) {

        

        [MyUnitilty MBProgressHUD:_superController andContent:@"没有查询到数据!"];

        return;

    }

    for (AllSellerModel *model in AllpersonArr) {//判断所选的人员是否是列表存在的

        if ([model.SellerName isEqualToString:selectPersonStr]) {

            if ([self.sellerDelegate respondsToSelector:@selector(selectData:)]) {

                [self.sellerDelegate selectData:model];

            }

        }

    }

    

    [self removeFromSuperview];

}

#pragma mark - UITextFieldDelegate

- (void)textFieldChang:(UITextField *)textField{

    if (textField.tag == 100) {

        BOOL isChineseBool  = [MyUnitilty isChinese:textField.text];

        if ([textField.text isEqualToString:@""]) {

            selectPersonStr = @"";

            selectLab.text = [NSString stringWithFormat:@"已选择:%@",selectPersonStr];

            dataSource = [NSMutableArray arrayWithArray:AllpersonArr];

        }else if(isChineseBool){//筛选

            [self arrayContainString:textField.text];

            if(dataSource.count != 0){

                AllSellerModel *model = dataSource[0];

                selectPersonStr = model.SellerName;

                selectLab.text = [NSString stringWithFormat:@"已选择:%@",selectPersonStr];

            }else{

                selectPersonStr = @"";

                selectLab.text = [NSString stringWithFormat:@"已选择:%@",selectPersonStr];


            }

        }

        UITableView *tableview = [backgroundBtn viewWithTag:2];

        NSInteger height;

        if (dataSource.count<=5) {

            if (dataSource.count <=2) {

                height = 100;

            }else{

                height = dataSource.count*50;

            }

            

        }else{

            height = 300;

        }

        tableview.frame = CGRectMake(40, 215, WIDTH-80, height);

        [tableview reloadData];

    }

    

    BOOL isTableViewBool = NO;

    for (UIView *view in backgroundBtn.subviews) {

        NSLog(@"tag:%ld",(long)view.tag);

        if (view.tag == 2) {

            isTableViewBool = YES;

        }

    }

    if (isTableViewBool == NO) {

        UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(40, 215, WIDTH-80, 300)];

        tableview.delegate = self;

        tableview.dataSource = self;

        tableview.bounces = NO;

        tableview.backgroundColor = [UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:0.2];

        dataSource = [NSMutableArray arrayWithArray:AllpersonArr];

        [backgroundBtn addSubview:tableview];

        [tableview bringSubviewToFront:backgroundBtn];

    }

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    if ([string isEqualToString:@"\n"]) {

        [textField resignFirstResponder];

        return NO;

    }

    return YES;

}


- (void)arrayContainString:(NSString *)context {

    BOOL containbool;

    [dataSource removeAllObjects];

    for (int i = 0; i <AllpersonArr.count; i++) {

        AllSellerModel *model = AllpersonArr[i];

        containbool = [model.SellerName containsString:context];

        if (containbool) {

            [dataSource addObject:AllpersonArr[i]];

        }else{

            //            [selectPersonArr addObject:AllSellerArray[i]];

        }

    }

    

    

}


3.调用

-(void)onClickAction

{

    selectView = [[SelectSellerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    selectView.sellerDelegate = self;

    selectView.superController = self;

    [selectView initDataSource:[[AllSellerDB shareManager] selectAllData]];

    [self.view addSubview:selectView];

}


#pragma mark - 选择人员的协议


//代理协议

-(void)selectData:(AllSellerModel *)model{


  返回的数据

}

4.效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值