How to get indexPath in table view from button action in cell?

I have a table view with two actions in it, for the first I use the didSelectRowAtIndexPath delegate, for the second I would like to use the action on the button in my cell to do something else. My problem is that I cannot get the index path? What is the best practice for this.

+6
source share
5 answers

If you added a button to a cell like,

[cell.contentView addSubview:button]; 

then you can get the index path like,

 - (void)onButtonTapped:(UIButton *)button { UITableViewCell *cell = (UITableViewCell *)button.superview.superview; NSIndexPath *indexPath = [tableView indexPathForCell:cell]; // Go ahead } 
+9
source

I use this solution - I get the cell path using dots

 CGPoint center= [sender center]; CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; NSLog(@"%@",indexPath); 

His work is excellent

+2
source

here is the complete code for the custom button:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} //configure your cell here cell.titleLabel.text = @"some title"; //add button UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; resetButton.frame = CGRectMake(40, 10, 18, 18); [resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; [resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal]; [cell.contentView addSubview:myButton]; //afterwards return the cell return cell; - (void)onButtonTapped:(UIButton *)button { UITableViewCell *cell = (UITableViewCell *)button.superview.superview; NSIndexPath *indexPath = [filterTable indexPathForCell:cell]; NSLog(@"%@", indexPath); //use indexPath here... } 
+1
source

If you use a collection view, you can use the Yogesh method, but change indexPathForRowAtPoint for indexPathForItemAtPoint as follows:

 CGPoint center= [sender center]; CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; NSLog(@"%@",indexPath); 
0
source

If your button is moved or Apple changes the view hierarchy for accessories, this method goes up the chain to look for a UITableViewCell:

 - (void)tapAccessoryButton:(UIButton *)sender { UIView *parentView = sender.superview; // the loop should take care of any changes in the view heirarchy, whether from // changes we make or apple makes. while (![parentView.class isSubclassOfClass:UITableViewCell.class]) parentView = parentView.superview; if ([parentView.class isSubclassOfClass:UITableViewCell.class]) { UITableViewCell *cell = (UITableViewCell *) parentView; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath]; } } 
-1
source

Source: https://habr.com/ru/post/897511/


All Articles