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
相关推荐
提笔忘字的帝国4 小时前
【教程】macOS 如何完全卸载 Java 开发环境
java·开发语言·macos
北京自在科技5 小时前
苹果iOS 26.3实现跨安卓数据无缝迁移
android·ios·findmy
郑梓斌8 小时前
Luban 2 Flutter:一行代码在 Flutter 开发中实现图片压缩功能
flutter·ios
数据雕塑家10 小时前
【网络故障排查实战】多台机器互ping异常:MAC地址冲突引发的网络“薛定谔猫“现象
网络·macos
遥不可及zzz11 小时前
ios adjust Google 设备端转化衡量
ios
酒醉的胡铁14 小时前
uniapp解决video组件在ios上全屏页面旋转90度,组件旋转180度
ios·uni-app
TheNextByte115 小时前
将照片从Mac传输到Android 7 种可行方法
android·macos·gitee
张飞签名上架16 小时前
深度解析超级签:iOS 应用分发的便捷之选与风险权衡
ios·苹果签名·企业签名·苹果超级签名·tf签
提笔忘字的帝国16 小时前
【2026版】macOS 使用 Homebrew 快速安装 Java 21 教程
java·开发语言·macos