在实际开发工作中,我们经常会在自定义的Cell中布局一些按钮,并且很多时候我们会在点击这个按钮的时候使我们的UItableviewController跳转到下一界面,有的可能还要传值。那么如何使我们的控制器能够获知我们按下了cell的按钮呢?毫无疑问,这是个代理模式的典型应用场景。
首先我们先得定义一个cell。.h文件如下:
@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文件如下:
#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.select

本文介绍了在自定义Cell中设置按钮并处理点击事件的方法,通过代理模式实现控制器与Cell的交互,详细讲解了如何定义Cell及在UIViewController中设置代理来监听按钮点击,从而实现页面跳转和数据传递。
2971

被折叠的 条评论
为什么被折叠?



