How to get indexpath in prepareForSegue

前端 未结 4 1023
予麋鹿
予麋鹿 2020-12-04 22:02
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
    if ([segue.identifier isEqualToString:@\"Action\"])
    {
        NSIndexPath *indexP         


        
相关标签:
4条回答
  • 2020-12-04 22:17

    Like this

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
        FTGDetailVC *detailVC = (id)segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    
        FTGNote *note = self.notes[indexPath.row];
        [detailVC updateWithNote:note];
    }
    
    0 讨论(0)
  • 2020-12-04 22:30

    Swift 3.0 / iOS 10

    tableView.indexPathForSelectedRow was introduced in iOS 9

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let indexPath = tableView.indexPathForSelectedRow{
            let selectedRow = indexPath.row
            let detailVC = segue.destination as! ParkDetailTableVC
            detailVC.park = self.parksArray[selectedRow]
        }
    }
    
    0 讨论(0)
  • 2020-12-04 22:33

    sorry, i don't understand what you want to do. it's possible get the indexPath.row through this method of UITAbleViewDelegate that it's called when you tap the cell of your tableView:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     
        self.myVariable = indexPath.row
    }
    

    and then you can access to this value in the prepareForSegue in this way:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        //do something with self.myVariable
    }
    

    I hope i helped you.

    0 讨论(0)
  • 2020-12-04 22:38

    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];
          }
      }
      
    0 讨论(0)
提交回复
热议问题