kAiN
kAiN

Reputation: 2773

Delegate in Custom Cell

Hello everyone I'm trying to work with a custom delegate in my cell ...

In my file.h the custom cell I entered this:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate

- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;

@end


@interface FFCustomCellWithImage : UITableViewCell

@property (nonatomic, assign) id<FFCustomCellWithImageDelegate> delegate;

@property (strong, nonatomic) IBOutlet PFImageView *FotoPost;
@property (strong, nonatomic) IBOutlet PFImageView *FotoProfilo;
@property (strong, nonatomic) IBOutlet UILabel *NomeUtente;
@property (strong, nonatomic) IBOutlet UILabel *TestoPost;
@property (strong, nonatomic) IBOutlet UILabel *DataPost;
@property (strong, nonatomic) IBOutlet UIView *backgroundCell;
@property (strong, nonatomic) IBOutlet UIButton *LeggiCommentoButton;
@property (strong, nonatomic) IBOutlet UIButton *AddGoPoint;
@property (strong, nonatomic) IBOutlet UILabel *CountGoPoint;



@end

and in my file.m the custom cell I entered this

#import "FFCustomCellWithImage.h"
#import <Parse/Parse.h>

@implementation FFCustomCellWithImage
@synthesize delegate;


-(void)buttonPressed{    


    if (delegate && [delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
        [delegate customCell:self button1Pressed:self.AddGoPoint];
    }
}

the problem is that it gives me an error saying that respondsToSelector Know no instance method

Where is my mistake? sorry but I'm new to the delegates and am trying to learn

Upvotes: 1

Views: 4627

Answers (2)

Vincent Sit
Vincent Sit

Reputation: 2352

If you are using ARC, you'd better change

@property (nonatomic, assign) id<FFCustomCellWithImageDelegate> delegate;

to

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

and method

-(void)buttonPressed{    
    if (delegate && [delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
        [delegate customCell:self button1Pressed:self.AddGoPoint];
    }
}

to

-(void)buttonPressed{    


    if (self.delegate && [self.delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
        [self.delegate customCell:self button1Pressed:self.AddGoPoint];
    }
}

and

@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate

to

@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate <NSObject>

In UITableView Datasource method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    FFCustomCellWithImage *cell = (FFCustomCellWithImage *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[FFCustomCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        // Make sure you have this code
        cell.delegate = self;
    }

    return cell;
}

Upvotes: 3

JAManfredi
JAManfredi

Reputation: 777

The problem is because you declared your @protocol wrong, it should be:

@protocol FFCustomCellWithImageDelegate <NSObject>

- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;

@end

You were not giving it a super type of NSObject, therefore its not aware that respondsToSelector is a method of the FFCustomCellWithImageDelegate instance.

Also, if you are using ARC, your delegate property should be declared as (weak, nonatomic) you can read more about it here if need be (The post wasn't originally to explain that, but it contains it): Why are Objective-C delegates usually given the property assign instead of retain?

Upvotes: 3

Related Questions