#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: green: blue: 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: metrics:nil views:NSDictionaryOfVariableBindings(okBtn)];
NSArray *constraint_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[okBtn(44)]-5-|" options: 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: metrics:nil views:NSDictionaryOfVariableBindings(myTableView,okBtn)]; NSArray *tableView_V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[myTableView]-49-|" options: 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=;
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=;
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 ;
} -(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=; i<; 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

自定义带有uitableview的alertview对话框的更多相关文章

  1. 雷林鹏分享:jQuery EasyUI 窗口 - 自定义带有工具条和按钮的对话框

    jQuery EasyUI 窗口 - 自定义带有工具条和按钮的对话框 您可以创建一个带有工具栏(toolbar)和按钮(button)的对话框(dialog),可以从 HTML 标记创建.这个教程描述 ...

  2. 自定义带有图片的PreferenceActivity

    http://my.oschina.net/huangsm/blog/40027 和大家分享一下关于android中PreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给大 ...

  3. 带有关闭按钮的alertView

    概述 由于讨厌系统自带的alertView只能通过点击按钮才能关闭.你说万一按钮区域都是功能性的操作呢(这可不是我胡思乱想哦,要怪就产品的想法吧,呵呵哒),所以我们还是应该备有一个带有“X”(关闭按钮 ...

  4. iOS开发小技巧--自定义带有占位文字的TextView(两种方式)

    自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性 一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可 ...

  5. jQueryWEUI自定义对话框-带有textarea

    jQueryWEUI  示例下载 在jQueryWEUI中提供了很多类型的对话框, 可以去访问看一下. 今天记录的则是,自己定义的一个带有文本域的对话框,这样,可以不通过调转页面,实现一些信息的提交. ...

  6. Android开发2:事件处理及实现简单的对话框(Toast,AlertDialog,Snackbar,TextInputLayout的使用)

    前言 啦啦啦~又要和大家一起学习Android开发啦,博主心里好激动哒~ 在上篇博文中,我们通过线性布局和基础组件的使用,完成了一个简单的学生课外体育积分电子认证系统的界面,本篇博文,将和大家一起熟悉 ...

  7. 安卓 Dialogs(对话框)

    转载自:http://www.apkbus.com/home.php?mod=space&uid=679028&do=blog&id=61197 对话框是一个小的窗口用以提示用 ...

  8. JOptionPane如何自定义按钮绑定事件

    JOptionPane如何自定义按钮绑定事件 2018年01月29日 19:27:10 阅读数:475 摘自:https://blog.csdn.net/m0_37355951/article/det ...

  9. iOS学习31之UITableVIewCell自定义

    1. 自定义Cell 1> 为什么要自定义Cell UITableView 中系统的Cell共提供了四种默认样式,  分别是: UITableViewCellStyleDefault UITab ...

随机推荐

  1. Unix 环境高级编程---线程创建、同步、

    一下代码主要实现了linux下线程创建的基本方法,这些都是使用默认属性的.以后有机会再探讨自定义属性的情况.主要是为了练习三种基本的线程同步方法:互斥.读写锁以及条件变量. #include < ...

  2. 让MySQL支持中文

    这两天在学习webpy,把webpy的一个blog例子扒下来学习一下,默认创建的table当存入中文的时候是乱码,研究了一下这个问题. 1,创建table的时候就使用utf8编码 举个例子: crea ...

  3. Socket小项目的一些心得(鸣谢传智的教学视频)

    Socket是一种封装了四层通信的整体抽象入口,通常也称作"套接字",这是常用的四层通信这是访问Socket的流程图,这个分为客户端和服务器端,其中服务器端有以下步骤去建立,前面的 ...

  4. 100个直接可以拿来用的JavaScript实用功能代码片段(转载)

    把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaS ...

  5. Kettle 创建 Transformation

    1.第一步,先准备数据和工具 安装好mysql以及客户端工具 数据: USE `test`; CREATE TABLE `account` (   `id` int(11) NOT NULL AUTO ...

  6. Java Service Wrapper配置详解

    #encoding=UTF-8 # Configuration files must begin with a line specifying the encoding # of the the fi ...

  7. 最基本的Unix系统操作命令

    基本知识点: OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 USBHD ...

  8. IE6/IE7/IE8 FF常见问题解决

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-04-08) <style type="text/css" > *{ margin:0px a ...

  9. java.util.Date转java.sql.Date丢失时间问题

    java.sql.Date 只存储日期数据不存储时间数据// 会丢失时间数据preparedStatement.setDate(1, new java.sql.Date(date.getTime()) ...

  10. OpenCV 图像处理学习笔记(一)

    解读IplImage结构 typedef struct _IplImage { int nSize;                    /* IplImage大小 */ int ID;       ...