Search code examples
iosuitableviewnsindexpath

NSIndexPath return nil


I have a UIViewController with a table view. Table row contains buttons which trigger an action.

//This is my button selector inside tableview delegate cellForRowAtIndexPath

[myBtn addTarget:self action:@selector(onButtonPress:event:)forControlEvents:UIControlEventTouchUpInside];

//This is button action



 -(void)onButtonPress:(UIButton*)sender {
       CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
       NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

       DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
       controller.args=myArgs;
       [self.navigationController pushViewController:controller animated:YES];


 }

//this is my back button action

- (IBAction)onBackPressed:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}

At first time all is working fine but after i navigated to other ViewController and return back to tableview controller by pressing back button and again click on table row button the NSIndexPath is nil

i have tried this indexPathForRowAtPoint returns nil only for first cell in a uitableview


Solution

  • Try sending a delegate to the nextviewcontroller like

       DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
       controller.args=myArgs;
       controller.delegate = self;
       [self.navigationController pushViewController:controller animated:YES];
    

    and when traversing back from that viewController to the present table viewcontroller pass that delegate.

    This may help you...