#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. 通过ModuleImplAdvertisement向自定义服务传递参数

    无意中发现通过ModuleImplAdvertisement可以向自定义服务传递参数,有空试一试. —————————————————————————————————————————————————— ...

  2. svn's diff command

    [svn's diff command] svn diff 比较的是版本快照, 跟merge的应用diff完全不一样. 缺省情况下,svn diff忽略文件的祖先,只会比较两个文件的内容.如果你使用- ...

  3. 详解keil采用C语言模块化编程时全局变量、结构体的定义、声明以及头文件包含的处理方法

    一.关于全局变量的定义.声明.引用: (只要是在.h文件中定义的变量,然后在main.c中包含该.h文件,那么定义的变量就可以在main函数中作为全局变量使用) 方法1: 在某个c文件里定义全局变量后 ...

  4. 咏南多层开发框架支持最新的DELPHI 10 SEATTLE

    购买了咏南多层开发框架的老用户如有需要提供免费升级. 中间件

  5. 关于datatable的一些操作以及使用adapter对数据的操作

    private void updateToolStripMenuItem_Click(object sender, EventArgs e) {//将数据更新回数据库 //获取源数据 DataTabl ...

  6. Arduino 模拟信号的读入并转化为0-5V电压

    int ledIn = A0; void setup(){ Serial.begin(9600); } void loop(){ int sensorValue = analogRead(ledIn) ...

  7. 10个经典的Java main 方法面试题

    1. 不用main方法如何定义一个类? 不行,没有main方法不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7 开始就不行了. 2. main() ...

  8. 浏览器URL访问网页具体发生了什么

    [详细讲解:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html]   ——浏览器输入URL ——DNS解析过程 解析域名,找到 ...

  9. Spring+Struts+Ibatis的配置

    指定Spring配置文件位置 <context-param> <param-name>contextConfigLocation</param-name> < ...

  10. 【原创】省市二级联动纯javascript

    // 北京 上海 天津 重庆 河北 山西 内蒙古 辽宁 吉林 黑龙江 江苏 浙江 安徽 福建 江西 山东 河南 湖北 湖南 广东 广西 海南 四川 贵州 云南 西藏 陕西 甘肃 宁夏 青海 新疆 香港 ...