سؤال

I have two methods :

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
testNumber=indexPath.row;
    testNumber=testNumber+1;
    NSLog(@"Test number : %i",testNumber);
}

then

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"testStart1"])
   {
    tViewController *tvc = [segue destinationViewController];
    tvc.testNumberTVC=testNumber;
   }
}

I have got also a segue who is triggered by selecting a row in my UITableView.

My problem is when I select a row, prepareForSegue is acting before didSelectRowAtIndexPath so the new value of testNumber is not transferred.

I would like to implement prepareForSegue only when didSelectRowAtIndexPath is done or better: Transfer the value of testNumber using only didSelectRowAtIndexPath method and so removing prepareForSegue method.

I've seen few topics about transfering data from UITableView to DetailView but with the new xCode 5, when an error message appears, I don't really know if this is because solution is outdated or if there is a real error is my code.

هل كانت مفيدة؟

المحلول

You could create your segues between the view controllers rather than from cell selection and set the identifier of the segue in the storyboard. Then you could do something like this:

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
    testNumber=indexPath.row;
    testNumber=testNumber+1;
    NSLog(@"Test number : %i",testNumber);
    [self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
}

Another option would be to check out the answer to this link here. Basically you can get the cell indexPath in prepareForSegue like this:

  UITableViewCell *cell = (UITableViewCell*)sender;
  NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top