Skip index number between UITableView List

When I click an element of a specific list in my UITableView, I want to pass the index of the element I clicked to until the next detailed view.

This is my corresponding code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { HomeworkDetails *view = [[HomeworkDetails alloc] init]; int lastIndex = [indexPath indexAtPosition:[indexPath length] - 1]; NSLog(@"%u", lastIndex); [self performSegueWithIdentifier: @"HomeworkDetailsSegue" sender:self]; view.currentIndex = lastIndex; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

At this point, I can successfully move on to the next ViewController (HomeworkDetails), but no information is being transmitted. I am trying to accomplish this by doing view.currentIndex = lastIndex; but it does not work. How else can I do this? Sorry if I missed something; I'm starting iOS development.

With suggestions:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { HomeworkDetails *view = [[HomeworkDetails alloc] init]; int lastIndex = [indexPath indexAtPosition:[indexPath length] - 1]; NSLog(@"%u", lastIndex); [self performSegueWithIdentifier: @"HomeworkDetailsSegue" sender:self]; view.currentIndex = lastIndex; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"HomeworkDetailsSegue"]) { // note that "sender" will be the tableView cell that was selected UITableViewCell *cell = (UITableViewCell*)sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; //**error** HomeworkDetails *vc = (HomeworkDetails*)[segue destinationViewController]; vc.currentIndex = indexPath.row; } } 

Error from Compiler

+6
source share
2 answers

What you are describing is best done in the prepareForSegue:sender: method instead of didSelectRowAtIndexPath: In your storyboard, make sure your HomeworkDetailsSegue segue is configured between your tableView prototype cell and the destination view controller.

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"HomeworkDetailsSegue"]) { // note that "sender" will be the tableView cell that was selected UITableViewCell *cell = (UITableViewCell*)sender; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; HomeworkDetails *vc = (HomeworkDetails*)[segue destinationViewController]; vc.currentIndex = indexPath.row; } } 

Also, as a style, I would recommend renaming your HomeworkDetails class to HomeworkDetailsViewController. Doing this follows Apple conventions more closely and gives a clearer picture of the class.

+18
source

You must set the index property in your new view before transitioning it using the performSegueWityIdentifier function. And to take it one step further, an agreement on the use of best practice requires setting up a new view using the standard prepareForSegue method.

0
source

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


All Articles