自定义cell代理的用法

xiaoxiao2021-02-28  12

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

[objc]  view plain  copy @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文件如下:

[objc]  view plain  copy #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(00, [UIScreen mainScreen].bounds.size.widthself.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,代码如下:

[objc]  view plain  copy -(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哦!代理执行的代码如下:

[objc]  view plain  copy #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];   }  
转载请注明原文地址: https://www.6miu.com/read-2250195.html

最新回复(0)