自定义cell代理的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworld183/article/details/79121216

首先我们先得定义一个cell。.h文件如下:

[objc]  view plain  copy
  1. @protocol MycellDelegate <NSObject>  
  2.   
  3. @optional  
  4. -(void)didClickButton:(UIButton *)button;  
  5.   
  6. @end  
  7.   
  8. @interface Mycell : UITableViewCell  
  9.   
  10. +(instancetype)cellWithtableView:(UITableView *)tableview;  
  11.   
  12. @property(nonatomic,strong)DateModel *model;  
  13.   
  14. @property(nonatomic,weak) id<MycellDelegate> delegate;  

.m文件如下:

[objc]  view plain  copy
  1. #import "Mycell.h"  
  2.   
  3. @interface Mycell()  
  4.   
  5. @property(nonatomic,strong)UIButton *button;  
  6.   
  7. @end  
  8.   
  9. @implementation Mycell  
  10.   
  11. +(instancetype)cellWithtableView:(UITableView *)tableview  
  12. {  
  13.     static NSString *ID = @"cell";  
  14.     Mycell *cell = [tableview dequeueReusableCellWithIdentifier:ID];  
  15.     if(!cell)  
  16.     {  
  17.         cell = [[Mycell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];  
  18.         cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  19.         cell.textLabel.font = [UIFont systemFontOfSize:13.0];  
  20.     }  
  21.     return cell;  
  22.       
  23. }  
  24.   
  25.   
  26. //重写布局  
  27. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  28. {  
  29.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  30.     if(self)  
  31.     {  
  32.         self.button = [[UIButton alloc] initWithFrame:CGRectMake(00, [UIScreen mainScreen].bounds.size.widthself.frame.size.height)];  
  33.         [self.button setTitle:@"我是按钮点我" forState:UIControlStateNormal];  
  34.         [self.button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  
  35.         self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;  
  36.         self.button.titleLabel.font = [UIFont systemFontOfSize:12.0];  
  37.         [self.contentView addSubview:self.button];  
  38.         [self.button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];  
  39.     }  
  40.     return self;  
  41. }  
  42.   
  43.   
  44. //给cell控件赋值  
  45. -(void)setModel:(DateModel *)model  
  46. {  
  47.     self.textLabel.text = [NSString stringWithFormat:@"日期:%@、信息:%@",model.date,model.message];  
  48.       
  49. }  
  50.   
  51. #pragma mark - 按钮点击事件,通过代理模式响应  
  52. -(void)btnClick:(UIButton *)btn  
  53. {  
  54.     [self.delegate didClickButton:btn];  
  55. }  
  56.   
  57. @end  

上述代码定义了一个代理,当按下按钮时,代理响应。

现在回到UItableviewController,代码如下:

[objc]  view plain  copy
  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     Mycell *cell = [Mycell cellWithtableView:tableView];  
  4.     cell.model = self.Array[indexPath.row];  
  5.     cell.delegate = self;  
  6.     return cell;  
  7. }  

别忘了.delegate = self哦!代理执行的代码如下:

[objc]  view plain  copy
  1. #pragma mark - 代理事件  
  2. //跳转到下一界面并传值  
  3. -(void)didClickButton:(UIButton *)button  
  4. {  
  5.     Mycell *cell = (Mycell *)button.superview.superview;  
  6.     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];  
  7.     MessageController *vc = [[MessageController alloc]init];  
  8.     DateModel *model = self.Array[indexPath.row];  
  9.     vc.message = [NSString stringWithFormat:@"行号:第%ld行,日期:%@、信息:%@",(long)indexPath.row,model.date,model.message];  
  10.     [self.navigationController pushViewController:vc animated:YES];  
  11. }  

猜你喜欢

转载自blog.csdn.net/helloworld183/article/details/79121216