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
相关推荐
刘某某.1 小时前
macOS 终端美化完整教程(Ghostty + Zsh + Starship Catppuccin Powerline)
macos
咦呀2 小时前
macOS 上 SVN 报错 "SQLite 编译为 X.XX,运行时为 X.XX" 的完整解决方案
macos
秋雨梧桐叶落莳5 小时前
iOS——Masonry约束内容整理
开发语言·学习·macos·ios·objective-c·cocoa
pop_xiaoli5 小时前
【iOS】类和分类的加载
macos·ios·objective-c·cocoa
流年似水~6 小时前
iOS 开发进阶之路:从能跑到能维护
人工智能·程序人生·ios·语言模型
国科安芯6 小时前
空间辐射环境下抗辐射 MCU 可靠性机理及航空安全应用研究综述
单片机·嵌入式硬件·macos·无人机·cocos2d·risc-v
MonkeyKing6 小时前
iOS 音频会话 AVAudioSession 完整机制:分类、模式、激活策略
ios·音视频开发
一个人旅程~6 小时前
ARM版的windows(macbook虚拟机使用)在国内外技术平台有哪些版本可以选择?
windows·经验分享·macos·电脑
qq_327395037 小时前
MacOS安装openEMS
macos·openems
报错小能手19 小时前
Swift 并发 Combine响应式框架
开发语言·ios·swift