ios - 自定义单元格中的委托(delegate)

标签 ios uitableview delegates protocols

大家好,我正在尝试在我的单元格中使用自定义委托(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/

相关文章:

iOS AVFoundation : Get AVCaptureDevice. 格式的视频尺寸

ios - 如何在不违反 AutoLayout 约束的情况下隐藏 UITableViewCells

ios - 如何在Xamarin studio上的tableview上显示图像?

IOS6 UITabBarController didSelectViewController 事件

swift - 我应该如何将文本设置到表格 View 上的文本字段中?

ios - [NSConcreteTextStorage属性:atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString

ios - 检测 UITextField 安全文本输入覆盖的文本

iphone - 如何配置 friend 构建的.ipa?

ios - 如何使用 Swift 对字典数据进行分组

C# 如何将函数调用保存在内存中供以后调用