創(chuàng)建一個自定義的View
.h文件
#import <UIKit/UIKit.h>
@class CoinsTypeModel;
typedef void(^SelectCionsTypeBlock)(CoinsTypeModel? *? model);
@interface SelectCionsTypeView : UIView
@property (nonatomic, copy) SelectCionsTypeBlock??????? selectCionsBlock;
@property (nonatomic, copy) NSMutableArray??????? *????? cionsList;
-(void)show;
.m文件
#import "SelectCionsTypeView.h"#import "AppDelegate.h"#import "CionsTypeModel.h"@interface SelectCionsTypeView(){
? ? UIView? ? ? ? *? ? ? ? _darkView;
? ? UIView? ? ? ? *? ? ? ? _bottomView;
}
@property (nonatomic, strong) UITableView? ? ? ? *? ? ? ? ? tableView;
@end
@implementation SelectCionsTypeView
- (instancetype)init
{
? ? self = [super init];
? ? if (self) {
? ? ? ? [self setup];
? ? }
? ? return self;
}
- (void)setup {
? ? AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
? ? [app.window addSubview:self];
? ? UIView *darkView = [[UIView alloc]init];
? ? darkView.backgroundColor =RGBA(0, 0, 0, 0.5);
? ? darkView.alpha = 0;
? ? darkView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
? ? [self addSubview:darkView];
? ? _darkView = darkView;
? ? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
? ? [_darkView addGestureRecognizer:tap];
? ? UIView *bottomView = [[UIView alloc]init];
? ? bottomView.backgroundColor = [UIColor whiteColor];
? ? bottomView.alpha = 1;
? ? bottomView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT/2);
? ? [self addSubview:bottomView];
? ? _bottomView = bottomView;
? ? UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
? ? cancelBtn.frame = CGRectMake(0, _bottomView.height-50*KScaleH, _bottomView.width, 50*KScaleH);
? ? [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
? ? [cancelBtn setTitleColor:LINE_COLOR forState:UIControlStateNormal];
? ? [cancelBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
? ? ? ? [self dismiss];
? ? }];
? ? [_bottomView addSubview:cancelBtn];
? ? UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, cancelBtn.top-0.5, _bottomView.width, 0.5)];
? ? line.backgroundColor = LINE_COLOR;
? ? [_bottomView addSubview:line];
}
-(void)setCionsList:(NSMutableArray *)cionsList{
? ? _cionsList=cionsList;
? ? [self.tableView reloadData];
}
#pragma mark - public
- (void)show {
? ? [UIView animateWithDuration:0.2 animations:^{
? ? ? ? [self.superview endEditing:YES];
? ? } completion:^(BOOL finished) {
? ? ? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? ? ? _darkView.alpha = 0.5;
? ? ? ? ? ? _bottomView.frame = CGRectMake(0, SCREEN_HEIGHT-SCREEN_HEIGHT/2, SCREEN_WIDTH, SCREEN_HEIGHT/2);
? ? ? ? }];
? ? }];
}
#pragma mark - private
- (void)dismiss {
? ? [UIView animateWithDuration:0.3 animations:^{
? ? ? ? _bottomView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT/2);
? ? ? ? _darkView.alpha = 0;
? ? } completion:^(BOOL finished) {
? ? ? ? [self removeFromSuperview];
? ? }];
}
- (void)tapClick {
? ? [self dismiss];
}
#pragma mark - delegate & datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? return self.cionsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? static NSString *cellID = @"tagCell";
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
? ? if (!cell) {
? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
? ? }
? ? CionsTypeModel? * model=self.cionsList[indexPath.row];
? ? cell.textLabel.text=model.cn_name;
? ? return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
? ? if (self.selectCionsBlock) {
? ? ? ? self.selectCionsBlock(self.cionsList[indexPath.row]);
? ? }
? ? [self dismiss];
}
- (UITableView *)tableView {
? ? if (!_tableView) {
? ? ? ? UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height-50-0.5) style:UITableViewStylePlain];
? ? ? ? table.delegate = self;
? ? ? ? table.dataSource = self;
? ? ? ? [_bottomView addSubview:table];
? ? ? ? _tableView = table;
? ? }
? ? return _tableView;
}
@end
在ViewController里面調(diào)用
SelectCionsTypeView * typeView=[[SelectCionsTypeView alloc]init];
? ? ? ? [typeView show];
? ? ? ? typeView.cionsList=self.dataList;
? ? ? ? typeView.selectCionsBlock = ^(CoinsTypeModel *model) {
? ? ? ? ? ? self.coinsTypeModel=model;
? ? ? ? ? ? _tagIndex = 0;
? ? ? ? ? ? self.cionsTypeTF.text=self.coinsTypeModel.cn_name
? ? ? ? };
想要自定義彈出視圖的小伙伴們可以參考一下拟糕,非常簡單判呕,點擊彈出自定義的tableView,可以自定義Model進行賦值送滞,