Eray Geveci
Eray Geveci

Reputation: 1129

how can i get the section number of a selected custom TableViewCell?

how can i find out in which section i clicked? i need the number to send the item to another view:

- (IBAction)mapButton:(id)sender {

    UIButton * myButton = sender;
    int row=myButton.tag;

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];

    MapViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"mapView"];

    self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];



    detail.currentStandort = self.selectedStandort;

    [self.navigationController pushViewController:detail animated:YES];


}

Upvotes: 1

Views: 369

Answers (1)

Thyraz
Thyraz

Reputation: 2432

Is the mapButton a subview of the UITableViewCell? Then I would do the following:

NSView *contentView = [sender superview];
NSTableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [myTableView indexPathForCell:cell];

NSInteger section = cellIndexPath.section;

You can also use the row from the cellIndexPath, so you don't have to keep your tag value up to date (e.g. on reuse, reorder and delete) which makes your implementation less error-prone.

Upvotes: 2

Related Questions