Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get indexpath in prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender      {     if ([segue.identifier isEqualToString:@"Action"])     {         NSIndexPath *indexPath = [self.tbl indexPathForSelectedRow];         SecondViewController *destViewController = segue.destinationViewController;         destViewController.getString = [getArray objectAtIndex:indexPath.row];     } } 

i wanna to access the selected row index,but show null for every selected row. please help me?

like image 699
Pintu Rajput Avatar asked Oct 09 '14 12:10

Pintu Rajput


People also ask

How do I get indexPath from cell Swift?

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.

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.

How do I get last indexPath in Swift?

You can get the indexPath of the last row in last section like this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)]; Here, numberOfSections is the value you return from numberOfSectionsInTableView: method.


1 Answers

Two cases:

  1. Segue connected from the viewController

    Call segue from your didSelectRowAtIndexPath method, pass indexPath as sender

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     [self performSegueWithIdentifier:@"Action" sender:indexPath]; } 

    Then you can get indexPath as sender in prepareForSegue:sender: method

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  {     if ([segue.identifier isEqualToString:@"Action"])     {         NSIndexPath *indexPath = (NSIndexPath *)sender;         SecondViewController *destViewController = segue.destinationViewController;         destViewController.getString = [getArray objectAtIndex:indexPath.row];     } } 
  2. segue connected from the cell

    No need to implement didSelectRowAtIndexPath method and performSegueWithIdentifier:.You can directly get sender as UITableviewCell in prepareForSegue:sender: method.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  {     if ([segue.identifier isEqualToString:@"Action"])     {         NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];         SecondViewController *destViewController = segue.destinationViewController;         destViewController.getString = [getArray objectAtIndex:indexPath.row];     } } 
like image 191
Suhail kalathil Avatar answered Sep 23 '22 12:09

Suhail kalathil