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.
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.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
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];
}
[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
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.
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