![](/img/trans.png)
[英]Can I add an action to a customCell in a UICollectionView?
[英]I can not make work the action of the button in a CustomCell
我正在使用UICollectionView
並且為“單元”類CustomCell
創建了一個實例,在其中實例化了一個按鈕。 如果將動作放在“ CustomCell.m
”中, 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
這是在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.m中
#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!");
}
替換此行代碼:
@property (nonatomic, assign) id delegate;
有了這個:
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
我也設置了weak like屬性,因為使用assign分配了一個強大的對象,並且在UICollectionView
無法釋放您的Cell
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.