大家好,我正在尝试在我的单元格中使用自定义委托(delegate)...
在我的 file.h 中,我输入了以下自定义单元格:
#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
在我的 file.m 中,我输入了这个自定义单元格
#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];
}
}
问题是它给了我一个错误,说 respondsToSelector
不知道实例方法
我的错误在哪里?对不起,但我是委托(delegate)的新手,我正在努力学习
最佳答案
问题是因为您声明 @protocol 错误,它应该是:
@protocol FFCustomCellWithImageDelegate <NSObject>
- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;
@end
您没有给它一个 super 类型的 NSObject,因此它不知道 respondsToSelector 是 FFCustomCellWithImageDelegate 实例的方法。
此外,如果您使用 ARC,您的委托(delegate)属性应声明为 (weak, nonatomic),如果需要,您可以在此处阅读更多相关信息(该帖子最初不是为了解释这一点,但它包含它):Why are Objective-C delegates usually given the property assign instead of retain?
关于ios - 自定义单元格中的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20288954/