ios 选择 UITableViewCell 时如何进行推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22759167/
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-08-30 23:51:58  来源:igfitidea点击:

How to make a push segue when a UITableViewCell is selected

iosobjective-cuitableviewsegue

提问by Aviral Bajpai

I have a list of effects in a table view. I have created a top right bar button which does a push segue to another view controller which helps create a new effect. Now I want to add push segues to the table view cells so that the effects values maybe loaded on the add effects view controller and I can save the edited values.

我在表视图中有一个效果列表。我创建了一个右上角的栏按钮,它向另一个视图控制器推送segue,这有助于创建新的效果。现在我想向表格视图单元格添加推送segue,以便可以在添加效果视图控制器上加载效果值,并且我可以保存编辑的值。

The question is can I create a push segue programmatically? If not can I pass the effect through a prepareforsegue? If I try to use a prepareforsegue, I run into a problem where control dragging from the UITableViewdoes not let me create a push segue to the add effects view controller.

问题是我可以以编程方式创建 push segue 吗?如果不是,我可以通过 a 传递效果prepareforsegue吗?如果我尝试使用prepareforsegue,我会遇到一个问题,即从 拖动的控件UITableView不允许我创建到添加效果视图控制器的推送segue。

回答by meda

controldrag From View Controller to View Controller

control从视图控制器拖动到视图控制器

From View Controller to View Controlle!

从视图控制器到视图控制器!

You will need to give an identifier to your segue:

您需要为您的 segue 提供一个标识符:

enter image description here

在此处输入图片说明

Perform the segue:

执行转场:

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

Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue delegate:

现在事情就是这样,如果您需要将一些数据传递给该视图控制器,这将只执行转场。然后你必须实现以下 segue 委托:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"yourSegue"])
    {
        //if you need to pass data to the next controller do it here
    }
}

回答by Suragch

Swift

迅速

This answer shows how to pass data and is updated for Xcode 8 and Swift 3.

此答案显示了如何传递数据并针对 Xcode 8 和 Swift 3 进行了更新。

Here is how to set up the project.

这是设置项目的方法。

  • Create a project with a TableView. See herefor a simple example.
  • Add a second ViewController.
  • Control drag from the first view controller with the table view to the second view controller. Choose "Show" as the segue type.
  • 创建一个带有TableView. 请参阅此处了解一个简单的示例。
  • 添加第二个ViewController
  • 控制从带有表视图的第一个视图控制器到第二个视图控制器的拖动。选择“显示”作为转场类型。

enter image description here

在此处输入图片说明

  • Click the segue in the storyboard and then in the Attribute Inspector name the Identifier "yourSegue". (You can call it whatever you want but you also need to use the same name in the code.)
  • 单击故事板中的 segue,然后在属性检查器中将标识符命名为“yourSegue”。(您可以随意调用它,但您还需要在代码中使用相同的名称。)

enter image description here

在此处输入图片说明

  • (Optional) You can embed the everything in a Navigation Controller by selecting the first view controller in the storyboard and the going to Editor > Embed In > Navigation Controller.
  • (可选)您可以通过选择故事板中的第一个视图控制器并转到Editor > Embed In > Navigation Controller 将所有内容嵌入到导航控制器中

Code

代码

First View Controller with TableView:

带有 TableView 的第一个视图控制器:

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // ...


    // method to run when table view cell is tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // Segue to the second view controller
        self.performSegue(withIdentifier: "yourSegue", sender: self)
    }

    // This function is called before the segue
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // get a reference to the second view controller
        let secondViewController = segue.destination as! SecondViewController

        // set a variable in the second view controller with the data to pass
        secondViewController.receivedData = "hello"
    }
}

Second View Controller

第二个视图控制器

class SecondViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    // This variable will hold the data being passed from the First View Controller
    var receivedData = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        print(receivedData)
    }
}

For a more basic example about passing data between View Controllers see this answer.

有关在视图控制器之间传递数据的更基本示例,请参阅此答案

回答by skwashua

Here is another option, that doesn't require the use of didSelectRowAtIndexPath.

这是另一个选项,不需要使用 didSelectRowAtIndexPath。

You just wire up the segue in Interface Builder from the prototype cell to the destination.

您只需将 Interface Builder 中的 segue 从原型单元格连接到目的地。

From there you can simply:

从那里你可以简单地:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "AffiliationDetail", let destination = segue.destinationViewController as? AffiliateDetailViewController {
        if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) {
            var affiliation = affiliations[indexPath.row]
            destination.affiliation = affiliation
        }
    }
}

回答by Mayank Jain

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

回答by Vineesh TP

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewController *myVController = (MyViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"StoryBordIdentifier"];
    [self.navigationController pushViewController:myVController animated:YES];
}

回答by Calin Chitu

You have 2 options: ctrl+drag from the prototype cell to the new vc and then you'll segue from each cell and you have to implement prepare for segue.

您有 2 个选项:ctrl+drag 从原型单元格到新的 vc,然后您将从每个单元格开始转场,您必须实施准备转场。

Second option, in didselectrowatindexpath you can instantiate your new viewcontroller using self.storyboard instantiateviewcontrollerwithidentifier..., then call pushviewcontroller or presentviewcontroller, you don't need segues in storyboard or prepare for segue in this scenario.

第二个选项,在 didselectrowatindexpath 中,您可以使用 self.storyboard instantiateviewcontrollerwithidentifier... 实例化您的新视图控制器,然后调用 pushviewcontroller 或 presentviewcontroller,在这种情况下,您不需要故事板中的转场或准备转场。

回答by PunjabiCoder

You can use this method :

您可以使用此方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

You can perform a function on selection of a particular tableview cell in this.

您可以在此选择特定的 tableview 单元格时执行功能。

回答by Bhanu

You can do this by following methods:

您可以通过以下方法执行此操作:

Firstly you need to import another view controller in current view.

首先,您需要在当前视图中导入另一个视图控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

inside above method you have to call a method - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;like this:

在上面的方法中,您必须调用这样的方法- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

yourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController YourViewController animated:YES];

By this you can achieve push segue when UITableViewCellis selected..

通过这种方式,您可以在UITableViewCell选择时实现 push segue ..

Hope this may help you…!

希望这可以帮助你......!

回答by yonlau

Swift 3

斯威夫特 3

After following up Ahmed Daou's answer -> manually drag segue from vc to vc at storyboard

在跟进 Ahmed Daou 的回答后 -> 在情节提要中手动将 segue 从 vc 拖到 vc

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "SegueIdentifier", sender: nil)    
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SegueIdentifier" {
        //do something you want
    }
}

回答by Jarvan

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"segueIdentifier"]) {
        NSIndexPath *indexPath = (NSIndexPath *)sender;

    }
}