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
相关推荐
Thomas_YXQ7 小时前
Unity3D在ios平台下内存的优化详解
开发语言·macos·ios·性能优化·cocoa
FAFU_kyp10 小时前
RISC0_ZERO项目在macOs上生成链上证明避坑
开发语言·后端·学习·macos·rust
b20772111 小时前
Flutter for OpenHarmony 身体健康状况记录App实战 - 提醒设置实现
python·flutter·macos·cocoa·harmonyos
TheNextByte111 小时前
iPhone 与Android :有什么区别?
android·cocoa·iphone
claem13 小时前
Mac端 Python脚本创建与理解
开发语言·python·macos
zhyongrui14 小时前
SwiftUI 光晕动画性能优化:消除托盘缩放卡顿的实战方案
ios·性能优化·swiftui
TheNextByte115 小时前
如何通过 6 种方式删除 iPhone/iPad 上的文件
ios·iphone·ipad
花花鱼15 小时前
mac下的iphone镜像连接
macos·cocoa·iphone
WeiAreYoung17 小时前
uni-app Xcode制作iOS谷歌广告Google Mobile Ads SDK插件
ios·uni-app
无法长大17 小时前
Mac M1 环境下使用 Rust Tauri 将 Vue3 项目打包成 APK 完整指南
android·前端·macos·rust·vue3·tauri·打包apk