ios 在 UITableViewCell 上点击执行条件转场
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7942942/
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
Conditional segue performed on tap on UITableViewCell
提问by Darrarski
I'm working on some project for iOS 5 using Xcode 4.2. I have one UITableViewController and want to perform a segue when user tap on table cell, but destination view controller depends on action performed on that cell. For example, when user tap on cell I would like to load SomeViewController, but when user tap on the same cell in editing mode I would like to load AnotherViewController. Unfortunately, there is no way to configure multiple segues on same cell in Xcode 4.2 storyboard builder, or I just don't get it. Perhaps there is a way to create segue by hand, in code editor. Generally what I want to achieve is to provide user a way to "enter" the item represented by cell using one view controller and "edit" the item represented by the same cell using another view controller. Switching to the second view controller (editor) when in table-edit-mode only was my first though, but maybe there is a better way. Any help will be appreciated.
我正在使用 Xcode 4.2 为 iOS 5 开发一些项目。我有一个 UITableViewController 并希望在用户点击表格单元格时执行 segue,但目标视图控制器取决于对该单元格执行的操作。例如,当用户点击单元格时,我想加载 SomeViewController,但是当用户在编辑模式下点击同一单元格时,我想加载 AnotherViewController。不幸的是,无法在 Xcode 4.2 故事板构建器中的同一个单元格上配置多个 segue,或者我就是不明白。也许有一种方法可以在代码编辑器中手动创建 segue。通常我想要实现的是为用户提供一种使用一个视图控制器“输入”由单元格表示的项目并使用另一个视图控制器“编辑”由同一单元格表示的项目的方法。在表编辑模式下切换到第二个视图控制器(编辑器)只是我的第一个,但也许有更好的方法。任何帮助将不胜感激。
回答by Lemont Washington
While looking Storyboard, control-drag from your source View Controller to your Destination view controller. This will create a segue that you can trigger programmatically right from your source View Controller. Ensure that you give you Segue a name. This name is what you will pass into the source View Controller's performSegue:withIdentifier: method.
在查看 Storyboard 时,从源视图控制器控制拖动到目标视图控制器。这将创建一个 segue,您可以直接从源视图控制器以编程方式触发它。确保你给你 Segue 一个名字。该名称是您将传递给源视图控制器的 performSegue:withIdentifier: 方法的名称。
回答by Rhubarb
I'm adding an extra answer just because after reading the selected one above in many places other than this - and it isthe correct answer - I found myself wondering how to detect the touch that would ordinarily trigger the segue. You see, if you create your segue the usual way by ctrl-dragging from the table view cell to the next controller, then two things are done for you automatically.
我添加了一个额外的答案,只是因为在除此之外的许多地方阅读了上面选择的答案之后——这是正确的答案——我发现自己想知道如何检测通常会触发转场的触摸。你看,如果你通过 ctrl 从表格视图单元格拖动到下一个控制器以通常的方式创建你的转场,那么两件事会自动为你完成。
- the touch in the cell is detected
- the segue is performed
- 检测到单元格中的触摸
- 执行segue
but of course, you can't block the segue.
但当然,你不能阻止转场。
Now if you want to conditionallysegue, then (as mentioned in the other answers) you delete that segue and create a new one from the UITableViewController (drag it from the objects navigator rather than the storyboard) to the next controller - and give it a name.
现在,如果您想有条件地进行转场,那么(如其他答案中所述)您删除该转场并从 UITableViewController 创建一个新转场(将其从对象导航器而不是故事板拖动)到下一个控制器 - 并给它一个姓名。
Then - and this is the part I was missing - implement tableView:didSelectRowAtIndexPath
in your table view controller to perform the segue programmatically and conditionally, as below.
然后 - 这是我缺少的部分 -tableView:didSelectRowAtIndexPath
在您的表视图控制器中实现以编程方式和有条件地执行 segue,如下所示。
Note that you also need to identify your cell somehow so you know if the one you're interested in has been selected. You could do that by knowing the index path in a static table, but I prefer to set my cells unique identifier in IB (even though I don't need it for dequeueing since it's a static table) and checking it. That way if I move my cell up or down in the static table, I won't need to change this code.
请注意,您还需要以某种方式识别您的单元格,以便您知道是否已选择您感兴趣的单元格。您可以通过知道静态表中的索引路径来做到这一点,但我更喜欢在 IB 中设置我的单元格唯一标识符(即使我不需要它来出列,因为它是一个静态表)并检查它。这样,如果我在静态表中向上或向下移动单元格,就不需要更改此代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Find the selected cell in the usual way
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Check if this is the cell I want to segue from by using the reuseIdenifier
// which I set in the "Identifier" field in Interface Builder
if ([cell.reuseIdentifier isEqualToString:@"CellIWantToSegueFrom"]) {
// Do my conditional logic - this was the whole point of changing the segue
if (myConditionForSegueIsSatisfied) {
// Perform the segue using the identifier I was careful to give it in IB
// Note I'm sending the cell as the sender because that's what the normal
// segue does and I already had code counting on that
[self performSegueWithIdentifier:@"SegueIdentifer" sender:cell];
}
}
Note how I send the cell with the segue - the normal segue from the cell does that, and I had originally passed nil, and my code which had depended on it stopped working.
请注意我如何发送带有转场的单元格 - 来自单元格的正常转场会这样做,而我最初传递的是 nil,而我依赖于它的代码停止工作。
回答by Chris Holloway
This is an old question and some great correct answersbut I'll post some additional information I felt helpful when I encountered this:
这是一个老问题和一些很好的正确答案,但我会发布一些额外的信息,当我遇到这个问题时我觉得很有帮助:
If you create a segue in the storyboard by ctl+drag from SomeVC to AnotherVC, delete it.
In the object navigator, ctl+drag at the ViewController level (usually first in the hierarchy under the name of the VC) from SomeVC to AnotherVC. Give the segue a unique identifier.
Implement
tableView: didSelectRowAtIndexPath:
as described above.I found it helpful to use a
switch
statement intableView: didSelectRowAtIndexPath:
switch (indexPath.row) { case 0: [self performSegueWithIdentifier:@"yourIDHere" sender:self]; break; case 1: // enter conditional code here... break; case 2: // enter conditional code here... break; // ... for however many cells you require default: break;
}
如果您通过 ctl+drag 从 SomeVC 到 AnotherVC 在故事板中创建了一个 segue,请将其删除。
在对象导航器中,在 ViewController 级别(通常是 VC 名称下的层次结构中的第一个)ctl+drag 从 SomeVC 到 AnotherVC。给 segue 一个唯一的标识符。
tableView: didSelectRowAtIndexPath:
如上所述实施。我发现使用以下
switch
语句很有帮助tableView: didSelectRowAtIndexPath:
switch (indexPath.row) { case 0: [self performSegueWithIdentifier:@"yourIDHere" sender:self]; break; case 1: // enter conditional code here... break; case 2: // enter conditional code here... break; // ... for however many cells you require default: break;
}
Thanks for the great answers everyone.
感谢大家的精彩回答。
回答by vedrano
You can easily control opening of any segue in your custom UIViewController by implementing:
您可以通过实现以下方式轻松控制自定义 UIViewController 中任何 segue 的打开:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
return true // or false, depending on what you need
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
return true // or false, depending on what you need
}