I am displaying tableview from custom cell and its working fine.
But the problem is that UiButton that is created inside tableCell not responding to selector.
I swipe the Row for Editing Then I create UIButton in titleForDeleteConfirmationButtonForRowAtIndexPath .
That appears same time when Delete button, touching Delete button responds to commitEditingStyle: Then Delete Button and Add button Disappers. Again I swipe the Row then Add & Delete Button Appear If I Click on Add Button It always Responds to canEditRowAtIndexPath then didEndEditingRowAtIndexPath.
I can’t figure out why it’s not calling btnMethod. Please let me know how to make it that btnMethod can called from uibutton touchDown event.
Here is My Code:
- (void)tableView:(UITableView *)tableView1 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];
btn.hidden=YES;
[btn removeFromSuperview];
}
}
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btnAdd=[UIButton buttonWithType:UIButtonTypeCustom];
btnAdd.frame=CGRectMake(8, 6, 80, 30);
[btnAdd setTitle:@"Add" forState:UIControlStateNormal];
btnAdd.backgroundColor=[UIColor magentaColor];
btnAdd.titleLabel.textAlignment=UITextAlignmentCenter;
btnAdd.titleLabel.textColor=[UIColor whiteColor];
btnAdd.layer.masksToBounds=YES;
btnAdd.layer.cornerRadius=6.0;
btnAdd.layer.borderWidth=2.0;
btnAdd.layer.borderColor=[UIColor blackColor].CGColor;
btnAdd.tag=indexPath.row+433;
[btnAdd addTarget:self action:@selector(btnMethod:) forControlEvents:UIControlEventTouchUpInside];
btnAdd.userInteractionEnabled=YES;
[cell11.contentView addSubview:btnAdd];
return @"Drop Me";
}
- (BOOL)tableView:(UITableView *)tableView1 canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView*)tableView1 didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];
btn.hidden=YES;
[btn removeFromSuperview];
}
-(void)btnMethod:(id)sender
{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
}
I have created sample application. Its worked fine for me using your above code.
Two suggestion from my side
1: use [cell.contentView bringSubviewToFront:btnAdd];
Second suggestion will be a worst case
2: Try with GustureRecognizers