dhaya
dhaya

Reputation: 1522

uitableview touch event

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];

Upvotes: 0

Views: 212

Answers (3)

gauravds
gauravds

Reputation: 3011

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];

Upvotes: 0

Apurv
Apurv

Reputation: 17186

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...
}

Upvotes: 1

Rajkumar
Rajkumar

Reputation: 271

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...

Upvotes: 0

Related Questions