iOS实现带遮罩的弹出选项卡
在我们日常开发的过程中难免会碰到一些选项的需求,下面是我针对我们该次需求做的一个小的Demo,闲话不多说了,上图片,上代码。
这样在我们选择上面一个Cell进行点击的时候,我会通过一个代理把数据传递到下面的页面,下面是代码
// //LCAlertListView.h //MeiMeiDu // //Createdby韩伟佳on16/4/6. //Copyright©2016年LangCuang.Allrightsreserved. // #import@classLCAlertListView; @protocolLCAlertListViewDelegate -(void)alertListView:(LCAlertListView*)viewdidSelectedRow:(NSInteger)row; @end @interfaceLCAlertListView:UIView -(instancetype)initWithFrame:(CGRect)framedatas:(NSArray*)datas; -(instancetype)initWithFrame:(CGRect)framedatas:(NSArray*)datascount:(NSArray*)counts; @property(nonatomic,strong)id delegate; @end
下面是具体实现
//
//LCAlertListView.m
//MeiMeiDu
//
//Createdby韩伟佳on16/4/6.
//Copyright©2016年LangCuang.Allrightsreserved.
//
#import"LCAlertListView.h"
#import"NoFreeCell.h"
staticCGFloatTableViewHeight;
@implementationLCAlertListView{
UITableView*mTableView;
NSArray*tableData;
NSArray*visiableData;
NSArray*visiableCount;
UIButton*backgroundBtn;
}
-(instancetype)initWithFrame:(CGRect)framedatas:(NSArray*)datas{
if(self=[superinitWithFrame:frame]){
self.backgroundColor=[UIColorclearColor];
backgroundBtn=[[UIButtonalloc]initWithFrame:frame];
backgroundBtn.backgroundColor=RGBA(88,88,88,0.8);
[backgroundBtnaddTarget:selfaction:@selector(dismiss)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:backgroundBtn];
tableData=datas;
TableViewHeight=(datas.count+1)*44+20;
mTableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,TableViewHeight)style:UITableViewStylePlain];
[mTableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];
mTableView.delegate=self;
mTableView.dataSource=self;
[selfaddSubview:mTableView];
[UIViewanimateWithDuration:.25animations:^{
[mTableViewsetFrame:CGRectMake(0,kScreenHeight-TableViewHeight,kScreenWidth,TableViewHeight)];
}completion:^(BOOLfinished){
}];
}
returnself;
}
-(instancetype)initWithFrame:(CGRect)framedatas:(NSArray*)datascount:(NSArray*)counts{
if(self=[superinitWithFrame:frame]){
self.backgroundColor=[UIColorclearColor];
backgroundBtn=[[UIButtonalloc]initWithFrame:frame];
backgroundBtn.backgroundColor=RGBA(88,88,88,0.8);
[backgroundBtnaddTarget:selfaction:@selector(dismiss)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:backgroundBtn];
visiableData=datas;
visiableCount=counts;
TableViewHeight=(datas.count+1)*44+20;
mTableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,TableViewHeight)style:UITableViewStylePlain];
[mTableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];
mTableView.delegate=self;
mTableView.dataSource=self;
[selfaddSubview:mTableView];
[UIViewanimateWithDuration:.25animations:^{
[mTableViewsetFrame:CGRectMake(0,kScreenHeight-TableViewHeight,kScreenWidth,TableViewHeight)];
}completion:^(BOOLfinished){
}];
}
returnself;
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
if(tableData.count>0){
return[tableDatacount];
}elseif(visiableCount.count>0){
return[visiableCountcount];
}
returnnil;
}
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell;
NoFreeCell*doubleCell;
if([tableDatacount]<=3&&[tableDatacount]>0){
cell=[tableViewdequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
cell.textLabel.text=tableData[indexPath.row];
returncell;
}else{
staticNSString*identifier=@"cell0";
doubleCell=[tableViewdequeueReusableCellWithIdentifier:identifier];
if(doubleCell==nil){
doubleCell=[[NoFreeCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
doubleCell.visibleRoleLabel.text=visiableData[indexPath.row];
doubleCell.showVisibleRoleLabel.text=visiableCount[indexPath.row];
}
returndoubleCell;
}
}
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
NSIntegerrow=indexPath.row;
[selfdismiss:row];
}
-(void)dismiss:(NSInteger)row{
if(_delegate&&[_delegaterespondsToSelector:@selector(alertListView:didSelectedRow:)]){
[_delegatealertListView:selfdidSelectedRow:row];
}
[UIViewanimateWithDuration:.15animations:^{
[mTableViewsetFrame:CGRectMake(0,kScreenHeight,kScreenWidth,TableViewHeight)];
}completion:^(BOOLfinished){
[selfremoveFromSuperview];
}];
}
-(void)dismiss{
[UIViewanimateWithDuration:.15animations:^{
[mTableViewsetFrame:CGRectMake(0,kScreenHeight,kScreenWidth,TableViewHeight)];
}completion:^(BOOLfinished){
[selfremoveFromSuperview];
}];
}
@end
上面的NoFree文件只是一个自定义的Cell,我们可以根据自己的需求自己设计,就不上传了,最后我们说说用法:
LCAlertListView*alertListView=[[LCAlertListViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)datas:visibleRoleArraycount:visibleRoleCountArray]; alertListView.delegate=self; [[[self.viewsuperview]superview]addSubview:alertListView];
下面是代理传值的使用
#pragmamark-LCAlertListViewDelegate
-(void)alertListView:(LCAlertListView*)viewdidSelectedRow:(NSInteger)row{
if(didSelectedIndex==0){
testVisibleRole=visibleRoleArray[row];
}else{
testData=datas[row];
}
NSIndexPath*indexPath=[NSIndexPathindexPathForRow:didSelectedIndexinSection:0];
[_myTableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
这样,我们的AlertTableVIew就做好了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。