iOS代理实现tableViewCell中的button在viewController中响应
来源:互联网 发布:化妆学校 知乎 编辑:程序博客网 时间:2024/05/21 17:18
这是第二次写博客,内容是接着上一次的代理,作为一个初学者,我写这东西不是为了像这个行业的大神们一样为同行们指点迷津,仅仅是记录下来我的学习过程,以及解决一直以来困扰我的问题的办法,文采不好,请高手勿喷……
代理由刚开始的完全不懂,现在慢慢理解了,昨天又做了个小程序,是在tableView的cell中有自定义的button,然后点击cell上的button,需要在controller中处理事件,上代码:
cell.h
#import <UIKit/UIKit.h> // // 声明协议@protocol btnClickedDelegate <NSObject>// cell中的button的点击事件方法,在协议中定义,在viewController中-(void)cellBtnClicked:(int)section row:(int)row;@end@interface mainTableViewCell : UITableViewCell// 声明一个delegate的变量,用来引用btnClickedDelegate中的方法@property (nonatomic,weak) id<btnClickedDelegate> btnDelegate;-(instancetype)initWithIntNum:(int)section row:(int)row;@end
cell.m
#import "mainTableViewCell.h"@implementation mainTableViewCell-(instancetype)initWithIntNum:(int)section row:(int)row{ if (self == [super init]) {// NSLog(@"%d",section);// NSLog(@"%d",row); UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 150, 30)]; label.text = @"测试"; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor grayColor]; [self.contentView addSubview:label]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+20, CGRectGetMinY(label.frame), 50, 30)]; [btn setTitle:@"点击" forState:UIControlStateNormal]; btn.tag = section *100+row+1; [btn setBackgroundColor: [UIColor colorWithRed: 140/255.0 green:140/255.0 blue:140/255.0 alpha:1]]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:btn]; } return self;}//cell中的点击事件,调用协议中的方法-(void)btnClicked:(UIButton *)btn{ int tag = btn.tag; int section = tag/100; int row = tag%100; // 让viewController遵循协议,在viewController中具体实现该方法 [self.btnDelegate cellBtnClicked:section row:row];}@end
viewController.h
#import "ViewController.h"#import "mainTableViewCell.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,btnClickedDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UITableView *mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 240)]; mainTable.backgroundColor = [UIColor redColor]; mainTable.dataSource = self; mainTable.delegate = self; [self.view addSubview:mainTable];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 5;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * cellID = @"cellid"; mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { cell = [[mainTableViewCell alloc]initWithIntNum:indexPath.section row:indexPath.row]; // 指定cell的代理是自己 cell.btnDelegate =self; } return cell;}//实现代理中的方法,打印-(void)cellBtnClicked:(int)section row:(int)row{ NSLog(@"第%d块%d行被点击",section,row);}
这就是完整的代码,很简单,但是当时也是困扰了自己很久,正是应了那句话,难者不会,会者不难。。。所有的问题都是这样,所以当遇到难题的时候不要气馁,再硬的骨头只要你肯下嘴,最终也会啃下来的。。。
0 0
- iOS代理实现tableViewCell中的button在viewController中响应
- xib方式 在tableviewcell上添加button (代理方式)
- IOS 在viewcontroller中响应了点击事件后子视图无法响应点击事件
- iOS tableViewCell 代理
- iOS 将xib 视图中的视图拖拽到响应的ViewController中
- iOS 在TableViewCell中的UIImageView调整大小
- iOS 在TabViewController中的一个ViewController跳转到另一个ViewController
- tableViewCell只有一个button无响应
- iOS,关于真机tableViewCell上面button响应事件,不同版本崩溃问题解决
- ios中tableViewCell的长按实现
- iOS 之在一个viewController中添加多个viewController
- IOS中tableviewcell.frame.size返回的是cell在storyboard中的尺寸
- iOS 在viewController中改变状态栏颜色
- 在代码中使用stroyboard中的ViewController
- iOS -实现imageView中的button响应点击事件的方法
- unity3D,NGUI中button响应事件实现
- iOS之ios8中TableViewCell中的短线不能居中问题
- tableviewcell 上添加的button的响应方法
- Android Studio 与 NDK
- ECSHOP的订单状态在数据库中的表现
- JavaScript动态添加文本框
- MFC非模态对话框
- Android - 文件读写操作 总结
- iOS代理实现tableViewCell中的button在viewController中响应
- Hibernate映射关系(注解方式)
- 线性代数复习四——矩阵的维数和秩
- Java ArrayList、LinkedList和Vector的使用及性能分析
- eclipse 快捷键记载
- 填空题红白球概率问题
- 关于Myeclipse修改默认编码,而content types中update不能更新问题
- centos彻底删除mysql以及查看MySQL的安装路径
- js取float型小数点后两位数的方法