自定义Cell中按钮的点击事件
来源:互联网 发布:python return 多个值 编辑:程序博客网 时间:2024/05/01 06:24
在实际开发工作中,我们经常会在自定义的Cell中布局一些按钮,并且很多时候我们会在点击这个按钮的时候使我们的UItableviewController跳转到下一界面,有的可能还要传值。那么如何使我们的控制器能够获知我们按下了cell的按钮呢?毫无疑问,这是个代理模式的典型应用场景。
首先我们先得定义一个cell。.h文件如下:
@protocol MycellDelegate <NSObject>@optional-(void)didClickButton:(UIButton *)button;@end@interface Mycell : UITableViewCell+(instancetype)cellWithtableView:(UITableView *)tableview;@property(nonatomic,strong)DateModel *model;@property(nonatomic,weak) id<MycellDelegate> delegate;
.m文件如下:
#import "Mycell.h"@interface Mycell()@property(nonatomic,strong)UIButton *button;@end@implementation Mycell+(instancetype)cellWithtableView:(UITableView *)tableview{ static NSString *ID = @"cell"; Mycell *cell = [tableview dequeueReusableCellWithIdentifier:ID]; if(!cell) { cell = [[Mycell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.font = [UIFont systemFontOfSize:13.0]; } return cell; }//重写布局-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self) { self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.frame.size.height)]; [self.button setTitle:@"我是按钮点我" forState:UIControlStateNormal]; [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; self.button.titleLabel.font = [UIFont systemFontOfSize:12.0]; [self.contentView addSubview:self.button]; [self.button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; } return self;}//给cell控件赋值-(void)setModel:(DateModel *)model{ self.textLabel.text = [NSString stringWithFormat:@"日期:%@、信息:%@",model.date,model.message]; }#pragma mark - 按钮点击事件,通过代理模式响应-(void)btnClick:(UIButton *)btn{ [self.delegate didClickButton:btn];}@end
上述代码定义了一个代理,当按下按钮时,代理响应。
现在回到UItableviewController,代码如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ Mycell *cell = [Mycell cellWithtableView:tableView]; cell.model = self.Array[indexPath.row]; cell.delegate = self; return cell;}
别忘了.delegate = self哦!代理执行的代码如下:
#pragma mark - 代理事件//跳转到下一界面并传值-(void)didClickButton:(UIButton *)button{ Mycell *cell = (Mycell *)button.superview.superview; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; MessageController *vc = [[MessageController alloc]init]; DateModel *model = self.Array[indexPath.row]; vc.message = [NSString stringWithFormat:@"行号:第%ld行,日期:%@、信息:%@",(long)indexPath.row,model.date,model.message]; [self.navigationController pushViewController:vc animated:YES];}当我们的cell中按钮点击后,将会自动跳到这个代理方法中,我们获取到按钮所在的cell,通过indexPathforCel这个方法(系统API)可以获取到cell的行数,并拿到数据源,此时想要传值给下一个界面就变的非常简单。
2 0
- 自定义Cell中按钮的点击事件
- UITableView自定义cell上的按钮点击事件
- iOS 自定义Cell按钮的点击代理事件
- iOS 一一 自定义cell按钮的点击事件(代理)
- iOS 一一 自定义cell按钮的点击事件(通知机制)
- iOS 一一 自定义cell按钮的点击事件(KVO)
- 自定义cell中的按钮点击事件
- cell上按钮的点击事件
- ios tableView 自定义cell上的按钮点击事件不好用
- 获取cell中按钮的交互事件
- 自定义按钮点击事件
- 自定义Cell上的点击事件两种方法
- 点击cell中的按钮加载自定义视图
- ios tableviewcell上点击按钮禁用时点击走了cell点击事件的解决办法
- Android中按钮的点击事件监听
- popupwindow中按钮的点击事件
- Unity3D中Button的按钮点击事件
- iOS关闭UITableView中cell的点击事件
- JavaScript的格式--从格式做起,做最严谨的工程师
- Word中MathType菜单跑哪去了
- 35:Kafka源码解读中分区数、Consumer并行度等
- PureMVC 游戏框架解析
- Vvie前置摄像头浅析2——OpenVR API自带sample调试说明
- 自定义Cell中按钮的点击事件
- [2016/7/11][dp][dfs]Flood Fill Algorithm(to find the shortest path)
- uva 11270 Tiling Dominoes 轮廓线DP
- (4.6.14)android 插桩基本概念plugging or Swap
- hibernate缓存 2016-07-11
- delphi连接mysql
- sts配置问题
- 36:Kafka源码解读SocketServer下的Acceptor、Processor、Handler
- liteide 如何进行修改默认快捷键