didSelectRowAtIndexPath and prepareForSegue implementation

怎甘沉沦 提交于 2019-12-25 13:15:28

问题


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.


回答1:


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];


来源:https://stackoverflow.com/questions/21321433/didselectrowatindexpath-and-prepareforsegue-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!