您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页今天项目中遇到在自定义的cell中写Button,在ViewCo

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

来源:二三娱乐

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

写的不好请大家谅解。

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文

Copyright © 2019-2025 yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务