今天项目中遇到在自定义的cell中写Button,在ViewController实现方法的问题。

下面给大家介绍一下写法。
用代理tag值,或者用Block。都能实现,由于我项目中用的是代理,就给大家介绍一个代理的方法。

1:在Cell.h中添加代理。

//代理方法
@protocol ButtonClickDelegate <NSObject>

-(void)buttonDelegateClick:(UIButton *)btn;

@end

@interface ViewTableViewCell : UITableViewCell
@property (nonatomic, weak)UIImageView *imageVie;
@property(nonatomic,strong)Model*model;
@property(nonatomic,strong)UIButton*button;
-(void)buttonClick:(UIButton *)button;

@property(nonatomic,strong)id<ButtonClickDelegate>delegate;



2:在Cell.m中

-(void)buttonClick:(UIButton *)button
{
    
    if ([_delegate respondsToSelector:@selector(buttonDelegateClick:)])
    {
        button.tag=self.tag;
        [_delegate buttonDelegateClick:button];
        
        
    }

3:在ViewController

-(void)buttonDelegateClick:(UIButton *)btn
{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.imageCount =1; // 图片总数
    browser.currentImageIndex = btn.tag;
    browser.delegate = self;
    [browser show];
}

注意:一定要实现Cell的代理,要不然你会发现不走-(void)buttonDelegateClick:(UIButton *)btn

这个方法。。

cell.delegate=self;

写的不好请大家谅解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容