OGeek|极客世界-中国程序员成长平台

标题: ios - 未调用自定义委托(delegate)方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:18
标题: ios - 未调用自定义委托(delegate)方法

我正在尝试将 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)toggleOpenWithUserActionBOOL)userAction;

@end

@protocol IngredientHeaderViewDelegate <NSObject>

@property (nonatomic) NSInteger hoi;

@optional
- (void)sectionHeaderViewIngredientHeaderFooterView *)sectionHeaderView sectionOpenedNSInteger)section;
- (void)sectionHeaderViewIngredientHeaderFooterView *)sectionHeaderView sectionClosedNSInteger)section;

@end

在 IngredientHeaderFooterView.m 中:

- (void)toggleOpenWithUserActionBOOL)userAction {

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

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

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)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。会是什么?



Best Answer-推荐答案


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

- (void)sectionHeaderViewIngredientHeaderFooterView *)sectionHeaderView sectionOpenedNSInteger)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4