Reputation: 151
I created a button in uitableviewcell
@interface MyMusicLibraryCell : UITableViewCell
{
IBOutlet UIButton * rangeBtn ;
}
In .m file
@synthesize rangeBtn;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
rangeBtn = [[UIButton alloc] init];
rangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rangeBtn setBackgroundImage:[UIImage imageNamed:@"fav_4.png"] forState:UIControlStateNormal];
rangeBtn.frame = CGRectMake(260.0f, 10.0f, 20.0f, 20.0f);
[self addSubview:rangeBtn];
}
return self;
}
And I want using rangeBtn and add a addTarget in cellForRowAtIndexPath.how should I do it?
Upvotes: 1
Views: 654
Reputation: 3896
Add a tag
to the button and you can access the button as following:
// initWithStyle
rangeBtn.tag = 1;
// cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
UIButton *cellButton = (UIButton *)[cell viewWithTag: 1];
// add target to the button
[cellButton addTarget: self action: @selector(buttonPressed:) forControlEvent: UIControlEventTouchUpInside];
}
Upvotes: 1
Reputation: 1900
Set the rangeBtn.tag to some value (e.g 100).
Then in cellForRowAtIndexPath get reference to the button using: btn = [cell viewWithTag:100]
Now you can set the target for that button.
Upvotes: 0