要是我下面的代码能够帮到到家希望你们关注一下我的bog哦。。
以前实现过这个功能。后来项目需求去掉,,后来又要做到这一步,,好久没有搞了,刚刚一会就是不可以实现,后来就用block来解决了这个问题,做完需求以后还是想实现这个坑爹的代理来做到发现原来是少了一个设置代理,,奇葩的事情真多,,用代码来操作直接,下面我来用来里来是现在这个玩意
1,在自定义的cell里面来写一个代理吧
A 这个是在cell的.h里面的代码
#import <UIKit/UIKit.h>
// 制定协议:
@protocol TermCellDelegate <NSObject>
- (void)choseTerm:(UIButton *)button;
@end
@interface CustomCell : UITableViewCell
@property (assign, nonatomic) id<TermCellDelegate> delegate;//这个是代理属性
- (IBAction)sender:(id)sender;//这个是button按钮响应的方法
@end
B 那么我们就来.m里面的代码也给加上
- (IBAction)sender:(id)sender {
[_delegate choseTerm:sender];//这里时代理调用的方法
}
2 好了上面是cell里面制定代理操作。。接下来就是在控制器里面来实现一下咯
A 第一步还是控制器类里面要遵守代理方法这个不要多说
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,TermCellDelegate>别的tableView我就不多说了
这个方法就是遵守协议以后实现的代理方法哦
- (void)choseTerm:(UIButton *)button
{
NSLog(@"我这个代理方法已经实现了么??");
}
不!!!!还有一个就是你没有在cell里面设置代理。。我就掉到这个坑里面去了
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
/*if(cell == nil){
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}*/
//Person *per = self.dataArr[indexPath.row];
cell.delegate = self; 这个就是最重要的,不设置一下这个玩意就是坑爹,,我自认倒霉,,不设置这个就是不会来到这个里面调用到上面的那个代理方法
//cell.titleLabel.text = per.titleStr;
//cell.contentLabel.text = per.contentStr;
//cell.imgView.image = [UIImage imageNamed:per.imgName];
return cell;
}
好了这个是我的一点点小小的心得,,希望可以帮到到家