IOS 自定义代理协议Delegate

QuestionViewCell.h文件代码,定义代理协议

objectivec 复制代码
@protocol QuestionViewCellDelegate <NSObject>

- (void)cellIsOpenDidChangeAtIndexPath:(NSIndexPath *)indexPath;

@end

@interface QuestionViewCell : UITableViewCell

@property (nonatomic, weak) id<QuestionViewCellDelegate> delegate;


@end

QuestionViewCell.m文件调用代理代码

objectivec 复制代码
#import "QuestionViewCell.h"

@interface QuestionViewCell ()


@end

@implementation QuestionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [FontSizeUtil setupWidgets:@[self.titleLabel, self.contentLabel]
         pingfangWithFontSizes:@[@12, @11]];
}

- (IBAction)openClickAction:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellIsOpenDidChangeAtIndexPath:)]) {
        [self.delegate cellIsOpenDidChangeAtIndexPath:self.indexPath];
    }
}

@end

UITableView设置代理对象

objectivec 复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {、
    QuestionViewCell *cell = [tableView dequeue:QuestionViewCell.class forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}

ViewController.m 委托者实现代理协议

objectivec 复制代码
@interface TestViewController ()
<QuestionViewCellDelegate,
UITableViewDelegate,
UITableViewDataSource>
@end

@implementation TestViewController


#pragma mark - QuestionViewCellDelegate

- (void)cellIsOpenDidChangeAtIndexPath:(NSIndexPath *)indexPath {
    //刷新Row行
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end
相关推荐
韩俊强33 分钟前
Xcode16 Archive Error - Command SwiftCompile failed with a nonzero exit code
ios·xcode·swift·打包出错
月未央3 小时前
HarmonyOS Next 状态管理:Computed 装饰器实践
ios·harmonyos
獨枭3 小时前
在 macOS 上优化 Vim 用于开发
macos·vim·excel
甜可儿3 小时前
2min搞定~Mac Pro 编译安装 Nginx 1.8.1
运维·nginx·macos
1024小神4 小时前
mac利用“自动操作”扩展添加 Mac 访达右键菜单项
macos
禾川兴 1324240068812 小时前
国产芯片替代方案:解析沁恒以太网控制器芯片,内置MAC地址
单片机·嵌入式硬件·macos·桥接模式
Z灏14 小时前
mac安装navicat及使用
macos·navicat
1024小神17 小时前
Mac中nvm切换node版本失败,关闭终端再次打开还是之前的node
macos
二流小码农17 小时前
鸿蒙开发:权限管理之授权方式
android·ios·harmonyos
二流小码农18 小时前
鸿蒙开发:申请授权权限
android·ios·harmonyos