How to pass UITableView IndexPath to UIButton @selector by parameters in iOS?

I added UIButton to UITableViewCells . When the user clicks the button, we get the index path for using the values ​​from NSMutableArray . I used below to get the current IndexPath ,

 [getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside]; -(void) resendTheErrorMessage:(id)sender { NSLog(@"SEnder: %@", sender); //NSLog(@"Index Path : %@", indexpath); } 

Can someone help me pass the current index path of UIButton's @selector. Thanks in advance.

EDIT:

This is the result I got from NSLog()

 <UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>> 
+8
ios objective-c parameters uitableview uibutton
source share
5 answers

Add Your UIButton Like This

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setFrame:CGRectMake(10.0, 2.0, 140.0, 40.0)]; [btn setTitle:@"ButtonTitle" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [btn setTag:indexPath.row]; [cell.contentView addSubview:btn]; 

And then get its tag number -

 -(void)buttonClicked:(id)sender { NSLog(@"tag number is = %d",[sender tag]); //In this case the tag number of button will be same as your cellIndex. // You can make your cell from this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0]; UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath]; } 

Note. The above solution will work if there is only 1 section in the TableView table. If your tableView has more than one section, you should know your section index or go below.

Alternative: 1

 UIView *contentView = (UIView *)[sender superview]; UITableViewCell *cell = (UITableViewCell *)[contentView superview]; NSIndexPath *indexPath = [tblView indexPathForCell:cell]; 

Alternative: 2

 CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView]; NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint]; UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath]; 
+30
source share

Given that the button is a cell view subview and that the table view knows the pointer path to the cell, something like this will work:

 - (UITableViewCell *)containingCellForView:(UIView *)view { if (!view.superview) return nil; if ([view.superview isKindOfClass:[UITableViewCell class]]) return (UITableViewCell *)view.superview; return [self containingCellForView:view.superview]; } - (IBAction)buttonClicked:(id)sender { UITableViewCell *containingCell = [self containingCellForView:sender]; if (containingCell) { NSIndexPath *indexPath = [self.tableView indexPathForCell:containingCell]; NSLog(@"Section: %i Row: %i", indexPath.section, indexPath.row); } } 
+6
source share

If you only need a table row, you can set the button tag property to the row number. If you need the whole path (with the section number), you will have to subclass UIButton and create a new property ("indexPath" will be a good name), which you will set when adding a button to the table row.

+2
source share

To present a table with several sections, a simpler solution would be to assign a tag:

 cell.button.tag = indexPath.section * 1000 + indexPath.row 

In the event handler:

 let section = sender.tag / 1000 let row = sender.tag % 1000 

Note that you need to multiply by a larger number than 1000 if any of your sections may contain 1000 or more lines.

+1
source share

works for me if you add a tag to a button in cellForRowAtIndexPath ...

 cell.button.tag = indexPath.row; 

and in the cell class get the tag ... For example:

  NSLog(@"tag number is = %d",[sender tag]); 

what you can know cell.row. well it works for me, sorry for my english.

0
source share

All Articles