1. No need to pass two arguments in button's @selector
to get indexPath:
. The button itself is enough.
I have already given an answer here - How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?
2. If you want to that just for your knowledge and another way to do so then here is it.
Make a custom class of UIButton
.
MyButton.h
@interface MyButton : UIButton
@property (nonatomic, retain) NSIndexPath *indPath;
@end
MyButton.m
#import "MyButton.h"
@implementation MyButton
@synthesize indPath = _indPath;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
And use this button
instead of UIButton
.
MyButton *btn = [[MyButton alloc] initWithFrame:CGRectMake(60.0, 2.0, 200.0, 40.0)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:[NSString stringWithFormat:@"Button-%d", indexPath.row] forState:UIControlStateNormal];
// Assign indexPath here
btn.indPath = indexPath;
[btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
Note: Don't use UIButtonButtonWithType here.
And this is the clicked:
method
-(void)clicked:(MyButton *)sender
{
NSLog(@"sender = %@", sender);
NSLog(@"sender indexPath = %@", sender.indPath);
}