Marco Manzoni
Marco Manzoni

Reputation: 707

Get IndexPath for selected accessory button in UITableView

I have a problem with the UItableViewCell accessory button. My accessory button is "connected" with a segue to another UIViewController. When I tap on an accessory button, I get the object from the model and I "pass" this object to the segue destinationViewController. This is my prepare for segue method:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"IdentityCard"])
    {
        NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
        [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.tableView.indexPathForSelectedRow.row]];
    }
}

The problem is that the NSLog at the beginning of the method (after the "if") show always the number 0! I tried to implement the method:

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

but the prepare for segue method is executed before this method, so I can't set any @property for "save" the indexPath. How can I solve this problem?

Upvotes: 5

Views: 2962

Answers (3)

feca
feca

Reputation: 1179

@Andres's solution is work for me but if I want to add the Add button to the navigationbar and I want to show the same viewcontroller with same segue identifier then index = [self.tableView indexPathForCell:sender].row; code dont work because the sender is not subclass of UITableViewCell. So here is my solution with @Andres code:

- (void) addItem: (id) sender {
    [self performSegueWithIdentifier: @"Identifier" sender: self];
}

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender {
    id selectedObject;
    if (![[sender class] isSubclassOfClass: [UITableViewCell class]]) {
        // create a new object
        selectedObject = [...];
    }
    else {
        selectedObject = [objectArray objectAtIndex: [self.tableView indexPathForCell: sender].row];
    }

    if ([[segue identifier] isEqualToString: @"Identifier"]) {
        // use the new or existing object
        [selectedObject doSonething];
    }
}

Upvotes: 0

Andres
Andres

Reputation: 201

In:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

...you have the 'sender' parameter that lets you know the Cell of the accessory that has been Tapped, so with:

index = [self.tableView indexPathForCell:sender].row;

You can get the index of it in order to use it.

Then you don't need to use the tableView's accessoryButtonTappedForRowWithIndexPath method.

Upvotes: 20

Marco Manzoni
Marco Manzoni

Reputation: 707

I fix the problem in this way: I connected the UIViewController to the destinationViewController (NOT FROM THE ACCESSORY BUTTON BUT FROM THE UIViewController).

I created a @property in the first View Controller:

@property (nonatomic) NSInteger indexPathRow;

then I modified the code like this:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Disclosure Tapped----> %i" ,indexPath.row);
    self.indexPathRow = indexPath.row;
    [self performSegueWithIdentifier:@"IdentityCard" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"IdentityCard"])
    {
        NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
        [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.indexPathRow]];
    }
}

Now it works!

Upvotes: 1

Related Questions