iOS 5 - 从一个 TableViewController 到另一个 TableViewController

发布于 2024-12-20 15:35:16 字数 993 浏览 4 评论 0原文

我试图从一个 TableViewController 选择一个详细信息披露按钮(或一行)并继续到另一个 TableViewController,而不将任何数据从第一个 TableViewController 传递到第二个 TableViewController。因此,第一个 TableViewController 几乎充当一个表,它将根据所选的详细信息公开按钮(或行)导航到不同的 TableViewController。

到目前为止,我执行此操作的代码如下所示:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // --> I believe I'm missing something here to access a specific row? <--



    }
}

我希望有人可以帮助我!

I'm trying to select a detail disclosure button (or a row) from one TableViewController and segue to another TableViewController, without passing any data from the first to the second TableViewController. So pretty much the first TableViewController acts as a table that will navigate to different TableViewControllers depending on the detail disclosure button (or row) selected.

So far my code for doing this looks like the following:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // --> I believe I'm missing something here to access a specific row? <--



    }
}

I hope someone can help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海螺姑娘 2024-12-27 15:35:16

您可以从 tableView:accessoryButtonTappedForRowWithIndexPath: 函数中的 indexPath 获取特定行。具体来说,被点击的行是indexPath.row。 (如果您的表有多个部分,那么您也可以从indexPath 中读取该部分,但听起来您只是在使用一个简单的表。)

您可以将当前行存储在变量中,然后在prepareForSegue 中读取它:发件人选择去哪里。

You get the specific row from the indexPath in the tableView:accessoryButtonTappedForRowWithIndexPath: function. Specifically, the row that was tapped was indexPath.row. (If your table has multiple sections, then you can read the section out of indexPath, too, but it sounds like you're just using a simple table.)

You can store the current row in a variable and then read it in prepareForSegue:sender to choose where to go.

久隐师 2024-12-27 15:35:16

UIViewController 的 -prepareForSegue:sender: 的文档说:

因为 Segues 可以从多个源触发,所以您可以使用
segue 和 sender 参数中的信息来定义上下文
用于segue并传递适当的信息。例如,如果
Segue 源自表视图,sender 参数将
识别用户点击的表格视图单元格。你可以用那个
在目标视图控制器上设置数据的信息。

鉴于此,我首先查看 sender 参数,并期望它是指向表格单元格的指针。如果您知道单元格应位于哪个表中,则可以使用 -indexPathForCell: 检索单元格的索引路径。

The documentation for UIViewController's -prepareForSegue:sender: says:

Because segues can be triggered from multiple sources, you can use the
information in the segue and sender parameters to define the context
for the segue and pass the appropriate information. For example, if
the segue originated from a table view, the sender parameter would
identify the table view cell that the user tapped. You could use that
information to set the data on the destination view controller.

Given that, I'd look first at the sender parameter and expect it to be a pointer to the table cell. If you know which table the cell should be in, you can use -indexPathForCell: to retrieve the cell's index path.

纵性 2024-12-27 15:35:16

您可以将 Segue 从视图控制器拖动到按下详细信息披露按钮时想要显示的视图。您可以从代码中触发 segue,如下所示

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

来自奥地利的干杯
马丁

you can drag segue from the view controller to the view you want to get shown when pressing the detail disclosure button. you can fire the segue from within your code as follows

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

Cheers from Austria
Martin

漆黑的白昼 2024-12-27 15:35:16

如果您的发送者对象是 tableviewcell,则以下代码片段应该有效:

NSIndexPath *indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender];

否则请尝试

NSIndexPath *indexPath = nil;
if ([sender isMemberOfClass:[UITableViewCell class]]) 
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender]; 
}
else if ([[sender superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[sender superview]];
}
else if ([[[sender superview] superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
}

If your sender object is a tableviewcell, the following code snippet should work:

NSIndexPath *indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender];

Otherwise try

NSIndexPath *indexPath = nil;
if ([sender isMemberOfClass:[UITableViewCell class]]) 
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender]; 
}
else if ([[sender superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[sender superview]];
}
else if ([[[sender superview] superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
}
梦年海沫深 2024-12-27 15:35:16

您在准备 segue 时使用的发送者可以是您想要的任何发送者。如果您想知道该行,请发送该行或索引路径:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // Row information is supplied as the sender
        NSIndexPath *indexPath = sender;


    }
}

如果您想使用实际行,则将其设为发送者

[self performSegueWithIdentifier:@"ShowPlaces" sender:[tableView cellForRowAtIndexPath:indexPath]];

The sender you use in prepare for segue can be whatever you want. If you want to know the row, send the row, or the index path:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // Row information is supplied as the sender
        NSIndexPath *indexPath = sender;


    }
}

If you want to use the actual row, then make that the sender

[self performSegueWithIdentifier:@"ShowPlaces" sender:[tableView cellForRowAtIndexPath:indexPath]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文