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
相关推荐
未来猫咪花9 分钟前
# Flutter状态管理对比:view_model vs Riverpod
flutter·ios·android studio
RR13351 小时前
macOS 升级 bash 到最新版本
macos
我在北京coding2 小时前
Tableau for mac 驱动
macos
檀越剑指大厂2 小时前
【高效开发工具系列】Blackmagic Disk Speed Test for Mac:专业硬盘测速工具
macos
咕噜企业签名分发-淼淼4 小时前
开发源码搭建一码双端应用分发平台教程:逐步分析注意事项
android·ios
阿福不是狗9 小时前
Python使用总结之Mac安装docker并配置wechaty
python·macos·docker
gen_9 小时前
mac环境下的python、pycharm和pip安装使用
python·macos·pycharm
键盘敲没电14 小时前
【IOS】GCD学习
学习·ios·objective-c·xcode
SY.ZHOU14 小时前
Significant Location Change
macos·ios·cocoa
buyue__17 小时前
MacOS解决局域网“没有到达主机的路由 no route to host“
macos