Galip
Galip

Reputation: 5455

Custom delegate method not called

I'm trying to adapt Apple's example of TableViewUpdates (expanding TableView Cells) to my own application, but I can't seem to get it to work.

I tried narrowing the problem down and I think I know where the problem lies now.

Apple uses a UITableViewController as the base controller for the view, but I have a UIViewController that has the UITableViewDelegate and DataSource as delegate methods. I added the HeaderViewDelegate to it like this:

@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

In 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];
    }
}

And in my UIViewController that implements the delegate:

- (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;
}

But respondsToSelector: always returns false. What could it be?

Upvotes: 2

Views: 652

Answers (1)

nburk
nburk

Reputation: 22731

In your SearchViewController, you need to implement both methods from the protocol IngredientHeaderViewDelegate:

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

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

Also, don't forget to actually assign the delegate in IngredientHeaderFooterView. Make sure that it's not nil when toggleOpenWithUserAction: is called.

If you make sure that the methods are implemented and the delegate is actually assigned you should be good :)

Upvotes: 2

Related Questions