xcode 表格视图单元格 Segue 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10263173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:02:19  来源:igfitidea点击:

Table View Cell Segues not Working

objective-cxcodeuitableview

提问by generaltsow

I'm trying to do something very simple: set up a segue so that when you click on a cell in a table it will take you to another View Controller. The problem, I believe, originates from the fact that the TableView these cells are in is embedded in a regular ViewController (as opposed to a TableViewController), and is one of two subviews in this ViewController.

我正在尝试做一些非常简单的事情:设置一个 segue,以便当您单击表格中的单元格时,它会将您带到另一个视图控制器。我相信,问题源于这些单元格所在的 TableView 嵌入在常规 ViewController(而不是 TableViewController)中,并且是此 ViewController 中的两个子视图之一。

As far as I can tell, I've set everything up correctly: I embedded the ViewController with the two subviews in a Navigation Contoller, set it to be the dataSource and delegate for the TableView, and created a push segue from a TableViewCell to my second View Controller in the storyboard. However, when the app is run and a user clicks a row in the table, it merely selects the row and the segue doesn't fire at all (I've debugged it and the prepareForSegue function isn't even being called).

据我所知,我已经正确设置了所有内容:我将 ViewController 与两个子视图嵌入到导航控制器中,将其设置为 TableView 的数据源和委托,并创建了一个从 TableViewCell 到我的推送segue故事板中的第二个视图控制器。但是,当应用程序运行并且用户单击表中的一行时,它仅选择该行并且 segue 根本不会触发(我已经调试了它并且甚至没有调用 prepareForSegue 函数)。

Is there something I'm missing here? Or is this for some reason not possible if the TableView is not the only view in its view controller?

有什么我在这里想念的吗?或者,如果 TableView 不是其视图控制器中的唯一视图,出于某种原因,这是不可能的吗?

回答by wcochran

I find that if I had recently wired the segue from the cell's accessory view, and later delete the segue and try to wire a new segue to the cell directly it does notwork (using Xcode 4.6.2) -- Xcode keeps connecting the segue to the accessory view (even if there isn't one)! The way I fixed it is by selecting the cell in IB and using the connection inspector to (1) delete the original "accessory action" segue and (2) directly wire the "selection" segue by dragging from the filled circle in the image below to the appropriate destination view controller.

我觉得,如果我最近从有线细胞的附件鉴于SEGUE,后来删除SEGUE,并尝试连接一个新的SEGUE直接它的电池工作(使用的Xcode 4.6.2) - Xcode中保持连接SEGUE到附件视图(即使没有)!我修复它的方法是通过选择 IB 中的单元格并使用连接检查器 (1) 删除原始的“附件操作”segue 和 (2) 通过从下图中的实心圆圈拖动直接连接“选择”segue到适当的目标视图控制器。

enter image description here

在此处输入图片说明

回答by LNI

This may or may not help you, but I ran into this issue because I had defined two different cell types, and had provided didSelectRowAtIndexPathimplementation. I had to add [self performSegueWithIdentifier:@"Whatever" sender:self]as part of didSelectRowAtIndexPathand the issue got resolved.

这可能对您有帮助,也可能无济于事,但我遇到了这个问题,因为我定义了两种不同的单元格类型,并提供了didSelectRowAtIndexPath实现。我不得不添加[self performSegueWithIdentifier:@"Whatever" sender:self]作为的一部分,didSelectRowAtIndexPath问题得到了解决。

If you are able to at least detect row selections, you may be able to take advantage of this method.

如果您至少能够检测行选择,则可以利用此方法。

回答by Rodinia

If you have customized the cell rendering, e.g.:

如果您自定义了单元格渲染,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

then ensure you are using the same cell identifier as specified for the prototype cell in ste story.

然后确保您使用的单元标识符与为 ste story 中的原型单元指定的相同。

So in this case the 'Identifier' of the cell which hooked up the segue should be set to 'Cell'.

因此,在这种情况下,连接 segue 的单元格的“标识符”应设置为“单元格”。

回答by GoZoner

If you are creating the StoryBoard in Xcode then do the following:

如果您在 Xcode 中创建 StoryBoard,请执行以下操作:

  1. Create a UITableViewController
  2. Add a prototype UITableViewCell
  3. Create the UIViewController that will be your segue target.
  4. Control-Click on the prototype UITableViewCell and drag to the segue target.
  1. 创建一个 UITableViewController
  2. 添加原型 UITableViewCell
  3. 创建将成为您的转场目标的 UIViewController。
  4. 按住 Control 单击原型 UITableViewCell 并拖动到 segue 目标。

That's it. You'll probably want to edit the characteristics of the UITableViewCell to be, for example, your subclass of UITableViewCell.

就是这样。例如,您可能希望将 UITableViewCell 的特征编辑为 UITableViewCell 的子类。

回答by GoZoner

I had the similar issue. Fix is 1. Write SegueIdentifier for the segue in Storyboard 2. Add the following line [self performSegueWithIdentifier:@"SegueIdentifier" sender:nil]; in didSelectRowAtIndexPath.

我有类似的问题。修复方法是 1. 为 Storyboard 中的 segue 编写 SegueIdentifier 2. 添加以下行 [self performSegueWithIdentifier:@"SegueIdentifier" sender:nil]; 在 didSelectRowAtIndexPath 中。

I hope this would help.

我希望这会有所帮助。

回答by Out of Orbit

Had the same problem. Segue from a prototype table view cell inside a regular View Controller would make the app crash. In case someone have the same problem.

有同样的问题。从常规视图控制器内的原型表视图单元格转场会使应用程序崩溃。如果有人有同样的问题。

My solution:

我的解决方案:

If you are using a push segue make sure your first View Controller with the table view is embedded in a Navigation Controller otherwise "push segues" wont work. Also point the segue at the destination View Controller, not its Navigation Controller!

如果您使用推式转场,请确保您的第一个带有表格视图的视图控制器嵌入在导航控制器中,否则“推式转场”将不起作用。还要将 segue 指向目标视图控制器,而不是它的导航控制器!

And as mentioned earlier, make sure your "segue identifiers" are correct.

如前所述,请确保您的“segue 标识符”是正确的。

at "didSelectRowAtIndexPath" I call "[self performSegueWithIdentifier:@"Your_destination_View_Controller" sender:self];

在“didSelectRowAtIndexPath”我调用“[self performSegueWithIdentifier:@"Your_destination_View_Controller" sender:self];