0

I have a button that should call the delegate method of tableview

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

when that button is tapped.

Can anyone tell me how to do this.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
surendher
  • 1,374
  • 3
  • 26
  • 52
  • Check this answer:http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically – slecorne Nov 06 '13 at 13:19
  • Create a method (somewhere) which is used by both that delegate method and that button action method, and use that instead of this hack. – trojanfoe Nov 06 '13 at 13:21

5 Answers5

2
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
karthika
  • 4,085
  • 3
  • 21
  • 23
0

A button won't be able to automatically call that method because it has no concept of a table view or index path, but if you create your own method you can call that method directly

- (void)buttonAction:(id)sender
{
    NSIndexPath *path = //path for row you want to select
    [self tableView:self.tableView didSelectRowAtIndexPath:path];
}
wattson12
  • 11,176
  • 2
  • 32
  • 34
0
[self tableView:_myTbl didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:_rowNeeded inSection:_sectionNeeded];
//call method, like user selected needed cell

[_myTbl selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
// show selected cell in UI
Vov4yk
  • 1,080
  • 1
  • 9
  • 13
0

If you want that when user press the button on your TableViewCell then you fire the Action what's happen when a cell is tapped , then you can do the following.

In your cellForRowAtIndexPath method , add a Gesture Recogniser on your button. Like this ,

cell.myButton.userInteractionEnabled = YES;
cell.myButton.tag = indexPath.row ;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
[cell.myButton addGestureRecognizer:tap]; 

Here is the handleTap method

- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"Handle Tap method");
    NSLog(@"Row Index : %d" , recognizer.view.tag);
    int row_index = recognizer.view.tag ;

    // Perform your Action woth row_index
}

And don't forget to add in your header file. Hope it helps.

ayon
  • 2,180
  • 2
  • 17
  • 32
0

For select entire row in table view cell check boxes :

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

take "i" value as for loop

byJeevan
  • 3,728
  • 3
  • 37
  • 60
NRV
  • 21
  • 1