IOS 5 - Segue from one TableViewController to another TableViewController

I am trying to select a disclosure button (or row) from one TableViewController and move to another TableViewController without transferring data from the first to the second TableViewController. Thus, the first TableViewController acts as a table that will navigate through the various TableViewControllers depending on the selected disclosure button (or row).

So far my code for this is as follows:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { NSUInteger rowNumber = [indexPath row]; NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber]; self.categoryData = [self.commuterDict objectForKey:categoryCode]; [self performSegueWithIdentifier:@"ShowPlaces" sender:self]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"ShowPlaces"]) { PlacesViewController *placesViewController = [segue destinationViewController]; // --> I believe I'm missing something here to access a specific row? <-- } } 

I hope someone can help me!

+4
source share
5 answers

You get a specific row from indexPath in the tableView: accessoryButtonTappedForRowWithIndexPath: function. In particular, the string that was being listened was indexPath.row. (If your table has several partitions, then you can also read the section from indexPath, but it looks like you are just using a simple table.)

You can save the current line in a variable and then read it in the prepareForSegue: sender file to choose where to go.

+2
source

The documentation for UIViewController -prepareForSegue:sender: states:

Since segues can be run from multiple sources, you can use the information in the segue and sender parameters to determine the context for the transition and pass the relevant information. For example, if segue originated from a table view, the sender parameter identify the table view cell that the user clicked. You can use this information to set data on the destination view controller.

Given this, I would first look at the sender parameter and expect it to be a pointer to a table cell. If you know which table the cell should be in, you can use -indexPathForCell: to retrieve the pointer to the cell.

+2
source

You can drag the segue from the view controller to the view you want to display by clicking the details button. you can run segue from your code as follows

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

Greetings from Austria Martin

0
source

If your sender object is a tableviewcell, the following code snippet should work:

 NSIndexPath *indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender]; 

Otherwise try

 NSIndexPath *indexPath = nil; if ([sender isMemberOfClass:[UITableViewCell class]]) { indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender]; } else if ([[sender superview] isMemberOfClass:[UITableViewCell class]]) { indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[sender superview]]; } else if ([[[sender superview] superview] isMemberOfClass:[UITableViewCell class]]) { indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; } 
0
source

The sender you use to prepare for segue can be anything you want. If you want to know a string, send a string or a pointer path:

 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { NSUInteger rowNumber = [indexPath row]; NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber]; self.categoryData = [self.commuterDict objectForKey:categoryCode]; [self performSegueWithIdentifier:@"ShowPlaces" sender:indexPath]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"ShowPlaces"]) { PlacesViewController *placesViewController = [segue destinationViewController]; // Row information is supplied as the sender NSIndexPath *indexPath = sender; } } 

If you want to use the actual string do that sender

 [self performSegueWithIdentifier:@"ShowPlaces" sender:[tableView cellForRowAtIndexPath:indexPath]]; 
0
source

Source: https://habr.com/ru/post/1385626/


All Articles