NOTE:我不需要任何关于使用 UITableview 的 didselect 委托发送数据的建议
myButton.h
#import <UIKit/UIKit.h>
@interface myButton : UIButton
{
id userData;
}
@property (strong,nonatomic) NSString* data1;
@end
我的按钮.m
#import "myButton.h"
@implementation myButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
我用于测试的自定义单元 CollectionView Cell.h
#import "myButton.h"
@interface testCollectionViewCell : UICollectionViewCell
@property (nonatomic,weak) IBOutlet myButton *serviceFav;
我用于测试的自定义单元 CollectionView Cell.m
#import "testCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation testCollectionViewCell
@synthesize serviceFav;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//self.serviceFav=[myButton buttonWithType:UIButtonTypeCustom];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}
@end
这是我的集合视图委托代码
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
testCollectionViewCell *cell = [testCollectionViewCell dequeueReusableCellWithReuseIdentifier:@"testCollectionViewCell" forIndexPath:indexPath];
cellData = [self.collectionData objectAtIndex:[indexPath row]];
cell.serviceFav.data1 = @"data1"; **//shows error here and kills**
[cell.serviceFav addTarget:self action:@selector(touchUpHandler:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
//----- 这不叫 ----我想叫它
-(void)touchUpHandler:(myButton*)sender
{
UIButton *button = (myButton *)sender; //instance of UIButton
NSLog(@"Data 1 = %@",sender.data1);
}