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
相关推荐
Ya-Jun3 小时前
常用第三方库:flutter_boost混合开发
android·flutter·ios
Dachui_112215 小时前
MacOS上如何运行内网穿透详细教程
macos
玫瑰花开一片一片16 小时前
Flutter IOS 真机 Widget 错误。Widget 安装后系统中没有
flutter·ios·widget·ios widget
Bruce_Liuxiaowei17 小时前
使用Python脚本在Mac上彻底清除Chrome浏览历史:开发实战与隐私保护指南
chrome·python·macos
烎就是我18 小时前
100行代码swift从零实现一个iOS日历
ios·swift
iFlyCai20 小时前
对Mac文字双击或三击鼠标左键没有任何反应
macos·计算机外设
鸿蒙布道师1 天前
鸿蒙NEXT开发通知工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
鸿蒙布道师1 天前
鸿蒙NEXT开发网络相关工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
請你喝杯Java1 天前
Mac上Cursor无法安装插件解决方法
macos·cursor·vsode