未调用自定义委托方法

时间:2014-12-15 13:34:06

标签: ios objective-c iphone uitableview delegates

我试图让Apple的TableViewUpdates示例(将TableView Cells扩展到我自己的应用程序),但我似乎无法让它工作。

我尝试缩小问题范围,我想我现在知道问题所在。

Apple使用UITableViewController作为视图的基本控制器,但我有一个UIViewController,它将UITableViewDelegate和DataSource作为委托方法。我像这样添加了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];
    }
}

在我实现委托的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。它可能是什么?

1 个答案:

答案 0 :(得分:2)

SearchViewController中,您需要实施协议IngredientHeaderViewDelegate中的两种方法:

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

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

另外,不要忘记在IngredientHeaderFooterView中实际分配代理人。在调用nil时确保它不是toggleOpenWithUserAction:

如果您确保实施这些方法并且实际分配了delegate,那么您应该做得很好:)