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
相关推荐
2501_915918413 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
笔沫拾光3 小时前
iOS 正式包签名指南
flutter·ios·ios签名
2501_9280946513 小时前
Ps 2025 图像编辑 Photoshop(Mac中文)
图像处理·macos·photoshop·ps
Magnetic_h17 小时前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Digitally19 小时前
将 iPhone 联系人转移到 Infinix 的完整指南
ios·cocoa·iphone
Cosmoshhhyyy1 天前
mac环境下安装git并配置密钥等
git·macos
imLix1 天前
RunLoop 实现原理
前端·ios
肥肥呀呀呀1 天前
mac 安卓模拟器 blueStacks
macos
csdn_aspnet1 天前
如何在 MacOS 上安装 SQL Server
macos·sqlserver
归辞...2 天前
「iOS」————设计架构
ios·架构