iOS代理实现tableViewCell中的button在viewController中响应

这是第二次写博客,内容是接着上一次的代理,作为一个初学者,我写这东西不是为了像这个行业的大神们一样为同行们指点迷津,仅仅是记录下来我的学习过程,以及解决一直以来困扰我的问题的办法,文采不好,请高手勿喷……
代理由刚开始的完全不懂,现在慢慢理解了,昨天又做了个小程序,是在tableView的cell中有自定义的button,然后点击cell上的button,需要在controller中处理事件,上代码:


cell.h

#import 

//  声明协议
@protocol btnClickedDelegate 

//  cell中的button的点击事件方法,在协议中定义,在viewController中
-(void)cellBtnClicked:(int)section row:(int)row;

@end

@interface mainTableViewCell : UITableViewCell

//  声明一个delegate的变量,用来引用btnClickedDelegate中的方法
@property (nonatomic,weak) id  btnDelegate;

@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 ()

@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);
}

这就是完整的代码,很简单,但是当时也是困扰了自己很久,正是应了那句话,难者不会,会者不难。。。所有的问题都是这样,所以当遇到难题的时候不要气馁,再硬的骨头只要你肯下嘴,最终也会啃下来的。。。

你可能感兴趣的:(Objective--C)