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
相关推荐
开心就好20252 小时前
Flutter iOS应用混淆与安全配置详细文档指南
后端·ios
一个人旅程~3 小时前
macOS装进移动硬盘成为双系统的操作方法
linux·经验分享·macos·电脑
mCell4 小时前
MacOS 下实现 AI 操控电脑(Computer Use)的思考
macos·agent·swift
一个人旅程~4 小时前
在M系列的macbook上如何使用VMware安装ARM版的Win11以及注意哪些问题?
linux·windows·经验分享·macos·电脑
开心就好20254 小时前
苹果iOS应用开发上架与推广完整教程
后端·ios
用户69371750013844 小时前
XChat 为什么选择 Rust 语言开发
android·前端·ios
MonkeyKing4 小时前
Objective-C Runtime 完整机制:objc_class /cache/bits 源码解析
前端·ios
小宋加油啊4 小时前
Mac vscode安装PCL
ide·vscode·macos
秋雨梧桐叶落莳6 小时前
【iOS】 AutoLayout初步学习
学习·macos·ios·objective-c·cocoa·xcode
UnicornDev6 小时前
从零开始学iOS开发(第六篇):协议与扩展 —— 写出灵活可复用的Swift代码
macos·objective-c·cocoa