大家好,我正在尝试在我的单元格中使用自定义委托...
在我的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 不知道实例方法
我的错误在哪里?对不起,但我是代表们的新手,我正在努力学习
答案 0 :(得分:3)
问题是因为您声明了@protocol错误,它应该是:
@protocol FFCustomCellWithImageDelegate <NSObject>
- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;
@end
你没有给它一个超类型的NSObject,因此它不知道respondsToSelector是FFCustomCellWithImageDelegate实例的一个方法。
此外,如果您使用ARC,您的委托属性应声明为(弱,非原子),如果需要,您可以在此处阅读更多相关内容(该帖子最初不是为了解释它,但它包含它): Why are Objective-C delegates usually given the property assign instead of retain?
答案 1 :(得分:3)
如果您使用ARC,则最好更改
@property (nonatomic, assign) id<FFCustomCellWithImageDelegate> delegate;
到
@property (nonatomic, weak) id<FFCustomCellWithImageDelegate> delegate;
和方法
-(void)buttonPressed{
if (delegate && [delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
[delegate customCell:self button1Pressed:self.AddGoPoint];
}
}
到
-(void)buttonPressed{
if (self.delegate && [self.delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
[self.delegate customCell:self button1Pressed:self.AddGoPoint];
}
}
和
@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate
到
@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate <NSObject>
在UITableView数据源方法中:
- (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;
}