使用单元格按钮动作传递多个参数 [英] Pass multiple parameter using cell button action
问题描述
注意:我不需要使用UITableview的didselect委托发送数据的任何建议
NOTE: i don't need any suggestion regarding sending data using didselect delegate of UITableview
myButton.h
#import <UIKit/UIKit.h>
@interface myButton : UIButton
{
id userData;
}
@property (strong,nonatomic) NSString* data1;
@end
myButton.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
我的自定义单元格,用于testCollectionViewCell.h
#import "myButton.h"
@interface testCollectionViewCell : UICollectionViewCell
@property (nonatomic,weak) IBOutlet myButton *serviceFav;
我的testCollectionViewCell.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
这是我的集合视图委托的代码
And here is my code for the collection view delegate
- (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);
}
推荐答案
我只是使用与Question中相同的过程制作了一个示例,并且它起作用了.
I just made a sample with the same process as in the Question and it worked.
链接: https://www.dropbox.com/s/lb2k7nzsytxjnl2/tabletest.zip?dl = 0
使用自定义按钮类
MyButton.h
#import <UIKit/UIKit.h>
@interface MyButton : UIButton
@property (strong,nonatomic) NSString* data1;
@property (strong,nonatomic) NSString* data2;
@end
MyButton.m
#import "MyButton.h"
@implementation MyButton
@end
为您的CustomCell
类创建IBOutlet
Make an IBOutlet to your CustomCell
Class
CustomTableViewCell.h
#import <UIKit/UIKit.h>
#import "MyButton.h"
@interface CustomTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet MyButton *btnLike;
@end
ViewController.m
#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell* cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
cell.btnLike.data1 = @"data1";
cell.btnLike.data2 = @"data2";
[cell.btnLike addTarget:self action:@selector(touchUpHandler:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void) touchUpHandler:(MyButton *)sender {
NSLog(@"Data 1 = %@",sender.data1);
NSLog(@"Data 2 = %@",sender.data2);
}
@end
在单元格中单击按钮时,它会转到选择器方法touchUpHandler
并在控制台上打印
On clicking on the button in the cell, it goes to the selector method touchUpHandler
and prints on console
2016-10-12 23:14:39.034 tabletest[28414:671128] Data 1 = data1
2016-10-12 23:14:39.035 tabletest[28414:671128] Data 2 = data2
这篇关于使用单元格按钮动作传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!