0

I created a button inside of UITableViewCell, and when I click a button ,I want go to author view control with index value.

 tapped  = [UIButton buttonWithType:UIButtonTypeCustom];

[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:@"button" forState:UIControlStateNormal];

NSInteger tet;

tet = indexPath.row; 
[tapped addTarget:self action:@selector(tapped:) forControlEvents: UIControlEventTouchDown];

[Cell addSubview:tapped];
dasdom
  • 13,975
  • 2
  • 47
  • 58
dhaya
  • 1,522
  • 13
  • 21
  • possible duplicate of [Detecting which UIButton was pressed in a UITableView](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – Vladimir Sep 17 '12 at 11:54

3 Answers3

1

You should tag a button with indexPath.row.

tapped.tag = indexPath.row;

Inside your event handling code, you should use that tag to find index.

-(void) tapped:(UIButton *)sender
{
    UIButton *btn = (UIButton *)sender;
    int index = btn.tag;
    //Do rest...
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
0

try this

-(void) tapped:(UIButton *)sender
{
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [myTableView indexPathForCell:clickedCell];
    int section = indexPath.section;
    // You get easily your index path row
    int row = indexPath.row;
    // push your controller 

}

first update ur code

use ur event UIControlEventTouchUpInside

tapped  = [UIButton buttonWithType:UIButtonTypeCustom];

[tapped setFrame:CGRectMake(0, 0, 320, 100)];
[tapped setTitle:@"button" forState:UIControlStateNormal];

NSInteger tet;

tet = indexPath.row; 
[tapped addTarget:self action:@selector(tapped:) forControlEvents: UIControlEventTouchUpInside];

[Cell addSubview:tapped];
gauravds
  • 2,931
  • 2
  • 28
  • 45
0

you can tag the button as follows :

tapped.tag = indexPath.row

And in the the tapped method You can use it as follows:

-(IBAction)tapped:(id)sender
{
 UIButton *btn = (UIButton *)sender;
int index = btn.tag;

}

use this index as per your requirement...

Rajkumar
  • 271
  • 1
  • 5