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
相关推荐
黄毛火烧雪下21 分钟前
ios打包需要的证书及步骤
macos·ios·cocoa
月未央2 小时前
HarmonyOS Next 状态管理:Monitor 装饰器实践
ios·harmonyos
追寻向上4 小时前
基于图像比对的跨平台UI一致性校验工具开发全流程指南——Android/iOS/Web三端自动化测试实战
android·ui·ios
MrZWCui4 小时前
iOS OC匹配多个文字修改颜色和字号
学习·macos·ios·objective-c·cocoa·xcode
Cao_Shixin攻城狮5 小时前
Flutter PopScope对于iOS设置canPop为false无效问题
flutter·ios·popscope
Macdo_cn13 小时前
Jump Desktop for Mac v9.0.94 优秀的远程桌面连接工具 支持M、Intel芯片
macos
ikmb17 小时前
mac用docker跑sql server
macos·docker·容器
Mr.NickJJ19 小时前
iOS底层原理系列04-并发编程
macos·ios·cocoa
Macdo_cn20 小时前
Microsoft Excel 2024 LTSC mac v16.95 表格处理软件 支持M、Intel芯片
microsoft·macos·excel
Ethan. L20 小时前
iOS 模块化架构设计:主流方案与实现详解
ios·架构