gpt4 book ai didi

ios - 未调用自定义委托(delegate)方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:30 25 4
gpt4 key购买 nike

我正在尝试将 Apple 的 TableViewUpdates(扩展 TableView 单元格)示例改编为我自己的应用程序,但我似乎无法让它工作。

我试着缩小问题的范围,我想我现在知道问题出在哪里了。

Apple 使用 UITableViewController 作为 View 的基本 Controller ,但我有一个 UIViewController,它具有 UITableViewDelegate 和 DataSource 作为委托(delegate)方法。我像这样向其中添加了 HeaderViewDelegate:

@interface SearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, IngredientHeaderViewDelegate>

IngredientHeaderFooterView.h:

#import <Foundation/Foundation.h>
#include <UIKit/UIKit.h>

@protocol IngredientHeaderViewDelegate;

@interface IngredientHeaderFooterView : UITableViewHeaderFooterView

@property (nonatomic, weak) IBOutlet UILabel *lblTitle;

@property (nonatomic, weak) IBOutlet id <IngredientHeaderViewDelegate> delegate;

@property (nonatomic) NSInteger section;

- (void)toggleOpenWithUserAction:(BOOL)userAction;

@end

@protocol IngredientHeaderViewDelegate <NSObject>

@property (nonatomic) NSInteger hoi;

@optional
- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionOpened:(NSInteger)section;
- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionClosed:(NSInteger)section;

@end

在 IngredientHeaderFooterView.m 中:

- (void)toggleOpenWithUserAction:(BOOL)userAction {

if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionOpened:)]) {
NSLog(@"Test1");
[self.delegate sectionHeaderView:self sectionOpened:self.section];
}
if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionClosed:)]) {
NSLog(@"Test2");
[self.delegate sectionHeaderView:self sectionClosed:self.section];
}
}

在我实现委托(delegate)的 UIViewController 中:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

IngredientHeaderFooterView *ingredientHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

IngredientDescriptionInfo *ingredientInfo = (self.sectionInfoArray)[section];
ingredientInfo.headerView = ingredientHeaderView;

ingredientHeaderView.lblTitle.text = ingredientInfo.play.name;
ingredientHeaderView.section = section;
ingredientHeaderView.delegate = self;

return ingredientHeaderView;
}

但是 respondsToSelector: 总是返回 false。会是什么?

最佳答案

在您的 SearchViewController 中,您需要从协议(protocol) IngredientHeaderViewDelegate 中实现这两个方法:

- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionOpened:(NSInteger)section
{
NSLog(@"section opened");
}

- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionClosed:(NSInteger)section
{
NSLog(@"section closed");
}

此外,不要忘记在 IngredientHeaderFooterView 中实际分配委托(delegate)。确保调用 toggleOpenWithUserAction: 时它不是 nil

如果您确保方法已实现并且 delegate 已实际分配,您应该很好:)

关于ios - 未调用自定义委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27485309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com