Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

ios - How to get indexpath in prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
    if ([segue.identifier isEqualToString:@"Action"])
    {
        NSIndexPath *indexPath = [self.tbl indexPathForSelectedRow];
        SecondViewController *destViewController = segue.destinationViewController;
        destViewController.getString = [getArray objectAtIndex:indexPath.row];
    }
}

i wanna to access the selected row index,but show null for every selected row. please help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two cases:

  1. Segue connected from the viewController

    Call segue from your didSelectRowAtIndexPath method, pass indexPath as sender

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

    Then you can get indexPath as sender in prepareForSegue:sender: method

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
        if ([segue.identifier isEqualToString:@"Action"])
        {
            NSIndexPath *indexPath = (NSIndexPath *)sender;
            SecondViewController *destViewController = segue.destinationViewController;
            destViewController.getString = [getArray objectAtIndex:indexPath.row];
        }
    }
    
  2. segue connected from the cell

    No need to implement didSelectRowAtIndexPath method and performSegueWithIdentifier:.You can directly get sender as UITableviewCell in prepareForSegue:sender: method.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
        if ([segue.identifier isEqualToString:@"Action"])
        {
            NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
            SecondViewController *destViewController = segue.destinationViewController;
            destViewController.getString = [getArray objectAtIndex:indexPath.row];
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...