Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
Marco Manzoni Avatar asked Nov 22 '12 17:11

Marco Manzoni


People also ask

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is IndexPath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

How do I create an IndexPath in Objective C?

To create an IndexPath in objective C we can use. NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ; To create an IndexPath in Swift we can use.


2 Answers

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.

like image 142
Andres Avatar answered Oct 26 '22 19:10

Andres


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!

like image 31
Marco Manzoni Avatar answered Oct 26 '22 20:10

Marco Manzoni