点击tableView的cell弹出tableView
2016-06-15 13:37
387 查看
在项目中总会用到点击大的TableView弹出一个小的TableView,将点击的小的TableViewcell内容传回大的TableViewCell上 最近项目需求 就写了一个demo 供大家参考
1、首先 我们建立一个View来承载这个小的tableview
@protocol HYJNegotiateViewDelegate<NSObject>
/**
* 建立代理方法 这里是将点击的cell内容记录下来
*/
-(void)HYJxxiangqingSizevviewGouwuChe :(NSString *)sender;
@end
@interface HYJNegotiateView :
UIView<UITableViewDataSource,UITableViewDelegate>
/**
* 显示选项
*/
@property (weak,
nonatomic) IBOutlet
UITableView *tableViewMy;
@property (weak, nonatomic) IBOutlet UITapGestureRecognizer *tapGesture;
/**
* 背景
*/
@property (weak,
nonatomic) IBOutlet
UIView *viewBackGround;
@property (strong,
nonatomic) NSArray *arrayData;
@property(nonatomic,weak)id<HYJNegotiateViewDelegate>delegate;
/**
* 这里是实例化方法 创建cell用
*/
+(HYJNegotiateView *)instanceSizeTextView;
在.m文件中进行编辑
+(HYJNegotiateView *)instanceSizeTextView
{
NSArray* nibView = [[NSBundle
mainBundle] loadNibNamed:@"HYJNegotiateView"
owner:nil
options:nil];
return [nibView
objectAtIndex:0];
}
-(void)awakeFromNib{
_tableViewMy.delegate =
self;
_tableViewMy.dataSource =
self;
_arrayData =
@[@"完成",@"未完成待料",@"未完成待修",@"未完成完工"];
_tableViewMy.tableFooterView = [[UIView
alloc]init];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *ID =@"UITableViewCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if (cell ==
nil) {
cell = [[UITableViewCell
alloc]initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:ID];
}
cell.textLabel.text =
_arrayData[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
return
44;
}
//在这里安全起见
对代理的参数方法进行判断
将点击的cell
获取到的内容(cell.textLabel.text)传回tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell*cell = [tableView
cellForRowAtIndexPath:indexPath];
if (self.delegate &&[self.delegate
respondsToSelector:@selector(HYJxxiangqingSizevviewGouwuChe:)]) {
[self.delegate
HYJxxiangqingSizevviewGouwuChe:cell.textLabel.text];
}
}
好 这个view我们就编写完了 我们接下来对控制器进行操作一下,这里我们用到了runtime
#import <objc/runtime.h>
static char cellKey;
头文件包含这些内容,在点击cell的方法中实现这个方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==
3) {
HYJNegotiateView *size =[HYJNegotiateView
instanceSizeTextView];
size.delegate =
self;
size.frame =
CGRectMake(0,
0, [UIScreen
mainScreen].bounds.size.width,[UIScreen
mainScreen].bounds.size.height);
[size.viewBackGround
addGestureRecognizer:size.tapGesture];
[size.tapGesture
addTarget:self
action:@selector(oneBtnsMethod)];
[[UIApplication
sharedApplication].keyWindow
addSubview:size];
self.negotiate = size;
objc_setAssociatedObject(self, &cellKey,
indexPath, OBJC_ASSOCIATION_RETAIN);//主要保存对indexPath的储存
}
}
点击cell之后就消失
-(void)oneBtnsMethod{
[self.negotiate
removeFromSuperview];
[self.negotiate.viewBackGround
removeFromSuperview];
}
在控制器中 实现代理方法
- (void)HYJxxiangqingSizevviewGouwuChe:(NSString *)sender{
NSIndexPath *indexPath = (NSIndexPath*)objc_getAssociatedObject(self,
&cellKey);
UITableViewCell *cell = [_tableViewMy
cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = sender;
[self
oneBtnsMethod];
}
控制器中的tableview的数据,cell展示方式可自行定制,在这里就不多讲了,我用的是系统的cell;
1、首先 我们建立一个View来承载这个小的tableview
@protocol HYJNegotiateViewDelegate<NSObject>
/**
* 建立代理方法 这里是将点击的cell内容记录下来
*/
-(void)HYJxxiangqingSizevviewGouwuChe :(NSString *)sender;
@end
@interface HYJNegotiateView :
UIView<UITableViewDataSource,UITableViewDelegate>
/**
* 显示选项
*/
@property (weak,
nonatomic) IBOutlet
UITableView *tableViewMy;
@property (weak, nonatomic) IBOutlet UITapGestureRecognizer *tapGesture;
/**
* 背景
*/
@property (weak,
nonatomic) IBOutlet
UIView *viewBackGround;
@property (strong,
nonatomic) NSArray *arrayData;
@property(nonatomic,weak)id<HYJNegotiateViewDelegate>delegate;
/**
* 这里是实例化方法 创建cell用
*/
+(HYJNegotiateView *)instanceSizeTextView;
在.m文件中进行编辑
+(HYJNegotiateView *)instanceSizeTextView
{
NSArray* nibView = [[NSBundle
mainBundle] loadNibNamed:@"HYJNegotiateView"
owner:nil
options:nil];
return [nibView
objectAtIndex:0];
}
-(void)awakeFromNib{
_tableViewMy.delegate =
self;
_tableViewMy.dataSource =
self;
_arrayData =
@[@"完成",@"未完成待料",@"未完成待修",@"未完成完工"];
_tableViewMy.tableFooterView = [[UIView
alloc]init];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *ID =@"UITableViewCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if (cell ==
nil) {
cell = [[UITableViewCell
alloc]initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:ID];
}
cell.textLabel.text =
_arrayData[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
return
44;
}
//在这里安全起见
对代理的参数方法进行判断
将点击的cell
获取到的内容(cell.textLabel.text)传回tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell*cell = [tableView
cellForRowAtIndexPath:indexPath];
if (self.delegate &&[self.delegate
respondsToSelector:@selector(HYJxxiangqingSizevviewGouwuChe:)]) {
[self.delegate
HYJxxiangqingSizevviewGouwuChe:cell.textLabel.text];
}
}
好 这个view我们就编写完了 我们接下来对控制器进行操作一下,这里我们用到了runtime
#import <objc/runtime.h>
static char cellKey;
头文件包含这些内容,在点击cell的方法中实现这个方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==
3) {
HYJNegotiateView *size =[HYJNegotiateView
instanceSizeTextView];
size.delegate =
self;
size.frame =
CGRectMake(0,
0, [UIScreen
mainScreen].bounds.size.width,[UIScreen
mainScreen].bounds.size.height);
[size.viewBackGround
addGestureRecognizer:size.tapGesture];
[size.tapGesture
addTarget:self
action:@selector(oneBtnsMethod)];
[[UIApplication
sharedApplication].keyWindow
addSubview:size];
self.negotiate = size;
objc_setAssociatedObject(self, &cellKey,
indexPath, OBJC_ASSOCIATION_RETAIN);//主要保存对indexPath的储存
}
}
点击cell之后就消失
-(void)oneBtnsMethod{
[self.negotiate
removeFromSuperview];
[self.negotiate.viewBackGround
removeFromSuperview];
}
在控制器中 实现代理方法
- (void)HYJxxiangqingSizevviewGouwuChe:(NSString *)sender{
NSIndexPath *indexPath = (NSIndexPath*)objc_getAssociatedObject(self,
&cellKey);
UITableViewCell *cell = [_tableViewMy
cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = sender;
[self
oneBtnsMethod];
}
控制器中的tableview的数据,cell展示方式可自行定制,在这里就不多讲了,我用的是系统的cell;
相关文章推荐
- Java Runtime Environment 5.0 Update 12 下载
- C#使用委托(delegate)实现在两个form之间传递数据的方法
- C#利用delegate实现Javascript的each方法
- Query中click(),bind(),live(),delegate()的区别
- 详解jquery事件delegate()的使用方法
- jQuery中 delegate使用的问题
- 浅谈jquery中delegate()与live()
- 由点击页面其它地方隐藏div所想到的jQuery的delegate
- jQuery中delegate与on的用法与区别示例介绍
- php set_magic_quotes_runtime() 函数过时解决方法
- Asp.Net 程序错误Runtime Error原因与解决
- JS delegate与live浅析
- jQuery事件 delegate()使用方法介绍
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- System 类 和 Runtime 类的常用用法介绍
- .NET笔记之:Delegate内部原理的分析
- jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解
- C#匿名方法与Delegate类型转换错误分析
- jQuery中delegate和on的用法与区别详细解析
- ios8 UITableView设置 setSeparatorInset UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)