gpt4 book ai didi

ios - 如何在 prepareForSegue 中获取索引路径

转载 作者:IT王子 更新时间:2023-10-29 07:42:13 27 4
gpt4 key购买 nike

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

我想访问选定的行 index,但为每个选定的行显示 null。请帮帮我?

最佳答案

两种情况:

  1. SegueviewController 连接

    调用segue来自你的 didSelectRowAtIndexPath方法,通过 indexPath作为sender

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

    然后你可以在prepareForSegue:sender:中获取作为发件人的indexPath方法

    - (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

    无需实现 didSelectRowAtIndexPath方法和performSegueWithIdentifier: .你可以直接得到sender作为UITableviewCellprepareForSegue:sender:方法。

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

关于ios - 如何在 prepareForSegue 中获取索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26278730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com