Two cases:
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];
}
}
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];
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…