美文网首页Objective-C
今天项目中遇到在自定义的cell中写Button,在ViewCo

今天项目中遇到在自定义的cell中写Button,在ViewCo

作者: MaShuai丨 | 来源:发表于2017-03-16 15:40 被阅读12次

下面给大家介绍一下写法。
用代理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;

写的不好请大家谅解。

相关文章

网友评论

    本文标题:今天项目中遇到在自定义的cell中写Button,在ViewCo

    本文链接:https://www.haomeiwen.com/subject/ourjnttx.html