【问题标题】:Selecting programmatically a cell on a tableview doesn't perform associated segue以编程方式选择 tableview 上的单元格不会执行关联的 segue
【发布时间】:2012-04-11 10:49:14
【问题描述】:

好吧,正如我在主题中所说,我的问题是我以编程方式选择 tableview 上的一个单元格,目的是自动执行相关联的 segue。我有代码在关联的 tableviewcontroller 的 viewWillAppear 上执行此操作。

结果是选择了单元格,但没有执行segue。你知道为什么吗?

当我在 viewWillAppear 上以编程方式执行 segue 以解决问题时,但我很好奇为什么如果选择单元格时不会自动执行 segue...

顺便说一句。 segue 必须没问题,因为当我选择单元格时,它可以正常工作。

更多信息:我正在开发一个 splitviewcontroller。左侧有tableviewcontroller,segue在右侧有一个viewcontroller

非常感谢!

卡洛斯

相关代码:

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

// Si no hay ninguna fila seleccionada, seleccionaremos la primera

// Obtenemos la fila seleccionada
NSIndexPath *filaSeleccionadaPath = [self.tableView indexPathForSelectedRow];

// Si no hay ninguna fila seleccionada
if (!filaSeleccionadaPath)
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

}

}

【问题讨论】:

  • 你试过了吗:[cell setSelected:YES animated:YES];
  • 与 [cell setSelected:YES animated:YES] 的结果相同;
  • 您的意思是您希望[UITableView selectRowAtIndexPath: animated: scrollPosition:] 导致调用代理的didSelectRowAtIndexPath 方法吗?那不会发生。见Automatically cell selected tableView

标签: ios uitableview segue


【解决方案1】:

UITableView 上的文档指定:

调用此方法不会导致代理收到 tableView:willSelectRowAtIndexPath: 或 tableView:didSelectRowAtIndexPath: 消息,也不会向观察者发送 UITableViewSelectionDidChangeNotification 通知。

因此它不会触发任何动作。如果您使用 segues,您可能希望在 UIViewController 类上使用 performSegueWithIdentifier:sender:(很可能是 self)。您可以指定标识符和发送元素(很可能是单元格)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2017-10-03
    • 2022-01-14
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多