标签:
#import <UIKit/UIKit.h> typedef void(^MyCompleteHandler) (NSString *selectString); @interface TJCustomAlertViewOrTableView : UIView { MyCompleteHandler completeHandler; } @property(nonatomic,strong)NSMutableArray *dataSoureArray; +(TJCustomAlertViewOrTableView *)shareInStance; +(void)showAlertViewOrTableViewandCompleteHandler:(MyCompleteHandler)myCompleteHander; -(void)showAlertViewOrTableViewandCompleteHandler:(MyCompleteHandler)myCompleteHander; @end #import "TJCustomAlertViewOrTableView.h" #define kcontentViewSize CGSizeMake(200.0f, 250.0f); @interface TJCustomAlertViewOrTableView ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate> { UITableView *myTableView; UIView *contentView; UIButton *okBtn; NSInteger selectRow; } @end @implementation TJCustomAlertViewOrTableView +(TJCustomAlertViewOrTableView*)shareInStance { static TJCustomAlertViewOrTableView *customAlertViewOrTableView; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ customAlertViewOrTableView=[[TJCustomAlertViewOrTableView alloc]init]; }); return customAlertViewOrTableView; } -(instancetype)init { if (self=[super init]) { [self setUp]; } return self; } -(void)setUp { self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f]; self.frame=[UIScreen mainScreen].bounds; CGRect frect=CGRectZero; frect.size=kcontentViewSize; frect.origin.x=[UIScreen mainScreen].bounds.size.width/4.0f; frect.origin.y=[UIScreen mainScreen].bounds.size.height/4.0f; contentView=[[UIView alloc]initWithFrame:frect]; contentView.backgroundColor=[UIColor whiteColor]; contentView.layer.masksToBounds=YES; contentView.layer.cornerRadius=8.0f; [self addSubview:contentView]; UITapGestureRecognizer *tapBackGround=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissAlertViewOrTableView:)]; tapBackGround.delegate=self; [self addGestureRecognizer:tapBackGround]; okBtn=[UIButton buttonWithType:UIButtonTypeCustom]; okBtn.translatesAutoresizingMaskIntoConstraints=NO; [okBtn setTitle:@"确定" forState:UIControlStateNormal]; [okBtn addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside]; [okBtn setBackgroundColor:[UIColor redColor]]; [contentView addSubview:okBtn]; NSArray *constraint_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[okBtn]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(okBtn)]; NSArray *constraint_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[okBtn(44)]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(okBtn)]; [contentView addConstraints:constraint_H]; [contentView addConstraints:constraint_V]; myTableView=[[UITableView alloc]init]; myTableView.translatesAutoresizingMaskIntoConstraints=NO; myTableView.delegate=self; myTableView.dataSource=self; [contentView addSubview:myTableView]; NSArray *tableView_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[myTableView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myTableView,okBtn)]; NSArray *tableView_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[myTableView]-49-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myTableView,okBtn)]; [contentView addConstraints:tableView_H]; [contentView addConstraints:tableView_V]; } -(void)dismissView:(UIButton *)sender { if (completeHandler) { completeHandler(_dataSoureArray[selectRow]); } [self hideView]; } -(void)dismissAlertViewOrTableView:(UITapGestureRecognizer *)tapGesture { CGPoint point=[tapGesture locationInView:self]; if (!CGRectContainsPoint(myTableView.frame, point)) { [self hideView]; } } -(void)showView { UIWindow *keyWindow=[UIApplication sharedApplication].keyWindow; [keyWindow addSubview:self]; contentView.alpha=0; contentView.transform=CGAffineTransformMakeScale(0.01f, 0.01f); [UIView animateWithDuration:0.3f animations:^{ contentView.alpha=1.0f; contentView.transform=CGAffineTransformMakeScale(1.0f, 1.0f); }]; } -(void)hideView { [UIView animateWithDuration:0.3f animations:^{ contentView.alpha=0; contentView.transform=CGAffineTransformMakeScale(0.01f, 0.01f); } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } +(void)showAlertViewOrTableViewandCompleteHandler:(MyCompleteHandler)myCompleteHander { [[TJCustomAlertViewOrTableView shareInStance] showAlertViewOrTableViewandCompleteHandler:myCompleteHander]; } -(void)showAlertViewOrTableViewandCompleteHandler:(MyCompleteHandler)myCompleteHander { completeHandler=myCompleteHander; [self showView]; } #pragma mark -UITableViewDataSource or UITableViewDelegate -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataSoureArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text=_dataSoureArray[indexPath.row]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0f; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectRow=indexPath.row; } -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if(touch.view==myTableView) { return YES; } return NO; } @end #import "ViewController.h" #import "TJCustomAlertViewOrTableView.h" @interface ViewController () { NSMutableArray *dataArray; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; dataArray=[NSMutableArray array]; for (int i=0; i<10; i++) { [dataArray addObject:[NSString stringWithFormat:@"test%d",i]]; } // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)showViewBtnClick:(UIButton *)sender { [TJCustomAlertViewOrTableView shareInStance].dataSoureArray=dataArray; // [[TJCustomAlertViewOrTableView shareInStance]showAlertViewOrTableViewandCompleteHandler:^(NSString *selectString) { // // NSLog(@"selectString=%@",selectString); // }]; [TJCustomAlertViewOrTableView showAlertViewOrTableViewandCompleteHandler:^(NSString *selectString) { NSLog(@"selectString=%@",selectString); }]; } @end
标签:
原文地址:http://www.cnblogs.com/thbbsky/p/4287903.html