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
相关推荐
harder3214 小时前
Swift 面向协议编程的 RMP 模式
开发语言·ios·mvc·swift·策略模式
YoungHong19924 小时前
Claude Code & 智谱GLM-5.1 环境配置指南 (Windows/macOS/Ubuntu)
windows·ubuntu·macos
leluckys5 小时前
Jenkins CI/CD 持续集成专题十二、iOS-Jenkins自动化打包集成
ios·ci/cd·jenkins
leluckys5 小时前
Jenkins CI/CD 持续集成专题八- jenkins 之 iOS 打包及上传至蒲公英
ios·ci/cd·jenkins
全栈攻略5 小时前
老版本Docker Desktop for Mac 历史版本下载大全(macOS 10.15/11/12)
macos·docker·容器
YoungHong19925 小时前
【图文】Codex接入Kimi K2/GLM-5.1 环境配置指南 (Windows/macOS/Ubuntu)
windows·ubuntu·macos
leluckys5 小时前
Jenkins CI/CD 持续集成专题十一、jenkins打包ios脚本证书
ios·ci/cd·jenkins
leluckys5 小时前
Jenkins CI/CD 持续集成专题十-jenkins 可以通过api 打ios包
ios·ci/cd·jenkins
leluckys5 小时前
Jenkins CI/CD 持续集成专题九 -Mac服务器上配置Jenkins实现iOS项目自动打包
macos·ci/cd·jenkins
壹方秘境5 小时前
作为开发者,我们需要的可能不是Wireshark那样的数据包分析工具,也不是Stream、ProxyPin那样的抓包工具
后端·ios