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
相关推荐
游戏开发爱好者89 小时前
带签名时间戳接口的重放与压力测试实战,用动态值在发送前现算签名
网络协议·计算机网络·网络安全·ios·adb·https·压力测试
mCell13 小时前
谁在为 AI 开车:Agent 浏览器生态调研(2026)
chrome·macos·agent
2501_916007471 天前
申请 iOS 推送证书并配置 APNs 群发推送教程
android·ios·小程序·https·uni-app·iphone·webview
weixin_403810131 天前
iOS自动化脚本怎么避免风控?无屏幕镜像截图原理详解
ios·自动化·跨境电商·苹果群控·苹果投屏·多媒体运营
wangruofeng1 天前
3 步把 MacBook 变成 7×24 远程主机,人在外面随时接管桌面
ios·agent·mac
weixin_403810131 天前
iOS免越狱自动化蓝牙HID方案:不装代理也能跑脚本
运维·ios·自动化·跨境电商·ai智能体·ios投屏·苹果投屏
sudebao点com1 天前
一次“Google 能打开,FlexTV 打不开”的 DNS 故障排查:从 curl 超时到完整证据链
windows·macos·https·cdn·dns·nslookup·网络排错
GitLqr1 天前
Flutter 3.47 与 Dart 3.13:你必须了解的迁移指南
flutter·ios·dart
冯汉栩2 天前
Swift Control View,Label渐变颜色(源码)
开发语言·ios·swift
jike_20262 天前
iPhone日历查看会议纪要APP推荐:按日期查找历史录音更方便
ios·语音识别·iphone