ios一个可滑动列表式的弹出选择框(封装过的)

来源:互联网 发布:疯帽子雾化器做丝数据 编辑:程序博客网 时间:2024/06/05 00:25
使用方法
#import"QCPopView.h"

@interfaceRosterViewController()<QCPopViewDelegate>


@property(nonatomic,strong)QCPopView*popView;
@property(nonatomic,strong)NSString  *popType;
@property(nonatomic,strong)NSString  *areaIdStr;



@end



[selfpopWithArr:self.areaNameArr];




#pragma mark - QCPopView弹出
-(void)popWithArr:(NSMutableArray*)arr{
   
   self.popView= [[QCPopViewalloc]initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height)];
   self.popView.QCPopViewDelegate=self;
    [self.popViewshowThePopViewWithArray:arr];

}

#pragma mark -QCPopViewDelegate
-(void)getTheButtonTitleWithIndexPath:(NSIndexPath*)indexPath{
   
   
    if ([self.popTypeisEqual:@"1"]) {
       
       self.areaIdStr= [selfsetPopButtonTitleWithTag:100andIndexPath:indexPathAndNameArr:self.areaNameArrAndIdArr:self.areaIdArr];
       
       NSLog(@"%@",self.areaIdStr);
    }
}
#pragma mark - QCpPopView设置按钮内容,和隐藏
-(NSString*)setPopButtonTitleWithTag:(NSInteger)tag andIndexPath:(NSIndexPath*)indexPath AndNameArr:(NSMutableArray*)nameArr AndIdArr:(NSMutableArray*)idArr{

   NSString*buttonStr = nameArr[indexPath.row];
   UIButton*button = (UIButton*)[self.viewviewWithTag:tag];
    [button  setTitle:buttonStrforState:UIControlStateNormal];
   NSString*idStr = [NSStringnew];
    idStr = idArr[indexPath.row];
    [self.popViewdismissThePopView];
    return idStr;
   

}








//
//  QCPopView.h
//  test
//
//  Created by乔超on 2017/8/8.
//  Copyright © 2017BoYaXun. All rights reserved.
//

#import<UIKit/UIKit.h>
@protocolQCPopViewDelegate <NSObject>

- (void)getTheButtonTitleWithIndexPath:(NSIndexPath*)indexPath;

@end

@interfaceQCPopView :UIView
/**
 * 内容视图
 */
@property(nonatomic,strong)UIView*contentView;
/**
 * 按钮高度
 */
@property(nonatomic,assign)CGFloatbuttonH;
/**
 * 按钮的垂直方向的间隙
 */
@property(nonatomic,assign)CGFloatbuttonMargin;
/**
 * 内容视图的位移
 */
@property(nonatomic,assign)CGFloatcontentShift;
/**
 * 动画持续时间
 */
@property(nonatomic,assign)CGFloatanimationTime;
/**
 * tableView的高度
 */
@property(nonatomic,assign)CGFloattableViewH;
@property(nonatomic,weak)id<QCPopViewDelegate> QCPopViewDelegate ;

/**
 * 展示popView
 *
 *  @param array buttontitle数组
 */
- (void)showThePopViewWithArray:(NSMutableArray*)array;
/**
 * 移除popView
 */
- (void)dismissThePopView;
@end




//
//  QCPopView.m
//  test
//
//  Created by乔超on 2017/8/8.
//  Copyright © 2017BoYaXun. All rights reserved.
//

#import"QCPopView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define LineColor [UIColor colorWithRed:242.0/255.0green:242.0/255.0blue:242.0/255.0alpha:1.0]


@interfaceButtonTableViewCell :UITableViewCell

@property(nonatomic,strong)UILabel*label;

@end

@implementationButtonTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
   self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
   if(self) {
       
       self.label= [[UILabelalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH-40,SCREEN_HEIGHT* (40.0/736.0))];
       self.label.textAlignment=NSTextAlignmentCenter;
       self.label.font= [UIFontsystemFontOfSize:14];
       
       
        [selfaddSubview:self.label];
       
       UIView*view = [UIViewnew];
        [selfaddSubview:view];
        view.backgroundColor=LineColor;
        view.frame=CGRectMake(0,SCREEN_HEIGHT* (40.0/736.0),SCREEN_WIDTH-40,1);
       
       
       
       
       
       
    }
   returnself;
}


@end







@interfaceQCPopView()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView*tableView;
@property(nonatomic,strong)NSArray*dataSource;
@property(nonatomic,strong)UIView*bgView;
@end
@implementationQCPopView

- (instancetype)initWithFrame:(CGRect)frame{
   if(self= [superinitWithFrame:frame]) {
       self.frame= frame;
       //初始化各种起始属性
        [selfinitAttribute];
       
        [selfinitTabelView];
    }
   returnself;
}

- (void)initTabelView{
   
   self.tableView= [[UITableViewalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH-40,self.contentShift)style:UITableViewStylePlain];
   self.tableView.layer.cornerRadius=10;
   self.tableView.delegate=self;
   self.tableView.dataSource=self;
   self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.tableViewregisterClass:[ButtonTableViewCellclass]forCellReuseIdentifier:@"cell1"];
   
    [self.contentViewaddSubview:self.tableView];
   
}



/**
 * 初始化起始属性
 */

- (void)initAttribute{
   
   self.buttonH=SCREEN_HEIGHT * (40.0/736.0)+1;
   self.buttonMargin=10;
   self.contentShift=SCREEN_HEIGHT * (250.0/736.0);
   self.animationTime=0.8;
   self.backgroundColor= [UIColorcolorWithWhite:0.614alpha:0.700];
   
    [selfinitSubViews];
}


/**
 * 初始化子控件
 */
- (void)initSubViews{
   
   self.contentView= [[UIViewalloc]init];
   self.contentView.backgroundColor= [UIColorwhiteColor];
   self.contentView.layer.cornerRadius=10;
   self.contentView.frame=CGRectMake(20,150,SCREEN_WIDTH-40,self.contentShift);
    [selfaddSubview:self.contentView];
   
}
/**
 * 展示pop视图
 *
 *  @param array需要显示buttontitle数组
 */
- (void)showThePopViewWithArray:(NSMutableArray*)array{
   UIWindow*window = [[UIApplicationsharedApplication].windowsfirstObject];
   
    [windowaddSubview:self];
   self.dataSource= array;
   
   
}

- (void)dismissThePopView{
   
   
    [selfremoveFromSuperview];
   
}

- (void)touchesBegan:(NSSet<UITouch*> *)touches withEvent:(UIEvent*)event{
   
   
    [selfdismissThePopView];
   
   
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
   returnself.dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
   
   
   ButtonTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"forIndexPath:indexPath];
   NSString* buttonStr = self.dataSource[indexPath.row];
    cell.label.text= buttonStr;
   
   returncell;
   
   
   
}
#pragma mark - UITableViewDelagate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
   returnself.buttonH;
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   
   if([self.QCPopViewDelegaterespondsToSelector:@selector(getTheButtonTitleWithIndexPath:)]) {
        [self.QCPopViewDelegategetTheButtonTitleWithIndexPath:indexPath];
    }
}









@end


阅读全文
0 0
原创粉丝点击