Ios 我无法使自定义单元格中按钮的操作正常工作
我使用的是Ios 我无法使自定义单元格中按钮的操作正常工作,ios,objective-c,uicollectionviewcell,Ios,Objective C,Uicollectionviewcell,我使用的是UICollectionView,我为我的“cell”类CustomCell创建了一个按钮,我在其中实例化了一个按钮。如果我将操作放在“CustomCell.m”中,则会响应日志并向我写入内容,但我需要使用类中包含UICollectionView的操作按钮 我使用它的原因是代表们,但我不工作 我该怎么修理 这是我添加到CustomCell的代码。h: @class CustomCell; @protocol CustomCellDelegate - (void)btnSaveColo
UICollectionView
,我为我的“cell”类CustomCell
创建了一个按钮,我在其中实例化了一个按钮。如果我将操作放在“CustomCell.m
”中,则会响应日志并向我写入内容,但我需要使用类中包含UICollectionView
的操作按钮
我使用它的原因是代表们,但我不工作
我该怎么修理
这是我添加到CustomCell的代码。h:
@class CustomCell;
@protocol CustomCellDelegate
- (void)btnSaveColor:(CustomCell *)sender;
@end
@interface CustomCell : UICollectionViewCell
@property (nonatomic, assign) id delegate;
@property (strong, nonatomic) IBOutlet UILabel *lblCustomCell;
@property (strong, nonatomic) IBOutlet UIView *bgCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *RCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *GCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *BCustomCell;
- (IBAction)btnSaveColor:(id)sender;
@end
这是我添加到CustomCell.m的代码
#import "CustomCell.h"
@implementation CustomCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)btnSaveColor:(CustomCell *)sender {
[_delegate btnSaveColor:self];
}
@end
#import "gradientViewController.h"
#import "ViewController.h"
#import "UIColor+Expanded.h"
#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
#import "ColorUtils.h"
#import "MPColorTools.h"
#import "RecipeCollectionHeaderView.h"
#import "UIColor+Colours.h"
@interface gradientViewController ()
@end
@implementation gradientViewController
- (void)btnSaveColor:(CustomCell *)sender {
NSLog(@"Funziona il delegato!");
}
这是在ViewController.h中输入的代码
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "CustomCell.h"
@interface gradientViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, CustomCellDelegate>
@property (nonatomic, strong) NSString *hexCode;
@property (strong, nonatomic) IBOutlet UILabel *lblDest;
@property (strong, nonatomic) IBOutlet UICollectionView *gradientCollectionView;
@property (strong, nonatomic) NSArray *gradientArray;
@property (strong, nonatomic) NSArray *gradientArrayToWhite;
@property (strong, nonatomic) NSArray *allGradient;
@property (strong, nonatomic) NSArray *reverse;
@property (strong, nonatomic) NSArray *sorted;
@property (strong, nonatomic) IBOutlet UIView *bgHEXColorGradient;
- (IBAction)btnBack:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *bgBack;
@property (nonatomic, strong) NSArray *ColorSchemeAnalagous;
@property (nonatomic, strong) NSArray *ColorSchemeComplementary;
@property (nonatomic, strong) NSArray *ColorSchemeTriad;
@end
#导入
#导入“ViewController.h”
#导入“CustomCell.h”
@界面gradientViewController:UIViewController
@属性(非原子,强)NSString*hexCode;
@属性(强,非原子)IBUILabel*lblDest;
@属性(强,非原子)IBUICollectionView*gradientCollectionView;
@性质(强,非原子)NSArray*梯度阵列;
@性质(强,非原子)NSArray*梯度阵列白色;
@性质(强,非原子)NSArray*allGradient;
@性质(强,非原子)NSArray*反向;
@属性(强,非原子)NSArray*已排序;
@属性(强,非原子)IBUIVIEW*BGHEXCOLORDEGRIDE;
-(iAction)btnBack:(id)发送方;
@属性(强,非原子)IBUIButton*bgBack;
@属性(非原子,强)NSArray*ColorSchemeAnalagous;
@属性(非原子,强)NSArray*colorschemecompletary;
@属性(非原子,强)NSArray*ColorSchemeTriad;
@结束
和ViewController.m中的这个
#import "CustomCell.h"
@implementation CustomCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)btnSaveColor:(CustomCell *)sender {
[_delegate btnSaveColor:self];
}
@end
#import "gradientViewController.h"
#import "ViewController.h"
#import "UIColor+Expanded.h"
#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
#import "ColorUtils.h"
#import "MPColorTools.h"
#import "RecipeCollectionHeaderView.h"
#import "UIColor+Colours.h"
@interface gradientViewController ()
@end
@implementation gradientViewController
- (void)btnSaveColor:(CustomCell *)sender {
NSLog(@"Funziona il delegato!");
}
#导入“gradientViewController.h”
#导入“ViewController.h”
#导入“UIColor+Expanded.h”
#导入“CustomCell.h”
#进口
#导入“ColorUtils.h”
#导入“MPColorTools.h”
#导入“RecipeCollectionHeaderView.h”
#导入“UIColor+colors.h”
@界面gradientViewController()
@结束
@gradientViewController的实现
-(无效)btnSaveColor:(自定义单元格*)发送方{
NSLog(@“funzionail delegato!”);
}
替换这一行代码:
@property (nonatomic, assign) id delegate;
为此:
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
@属性(非原子,弱)id委托;
我还设置了弱类属性,因为使用assign时,您有一个强对象,并且在
UICollectionView
中,当您将单元格设置为返回之前的末尾时,无法释放单元格。您将单元格委托设置为该单元格。委托=self@GianfrancoCotumaccio是的,是一样的,但这样做是好事:)谢谢你的帮助