How to make a push segue when a UITableViewCell is selected

IosObjective CUitableviewSegue

Ios Problem Overview


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.

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 UITableView does not let me create a push segue to the add effects view controller.

Ios Solutions


Solution 1 - Ios

control drag From View Controller to View Controller

From View Controller to View Controlle!

You will need to give an identifier to your 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:

- (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
    }
}

Solution 2 - Ios

Swift

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

Here is how to set up the project.

  • Create a project with a TableView. See here for 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.

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.)

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.

Code

First View Controller with 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.

Solution 3 - Ios

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

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

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
        }
    }
}

Solution 4 - Ios

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

Solution 5 - Ios

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

Solution 6 - Ios

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.

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.

Solution 7 - Ios

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.

Solution 8 - Ios

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 {}

inside above method you have to call a method `

  • (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;` like this:

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

By this you can achieve push segue when UITableViewCell is selected..

Hope this may help you…!

Solution 9 - Ios

Swift 3

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

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
    }
}

Solution 10 - Ios

- (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;
 
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAviral BajpaiView Question on Stackoverflow
Solution 1 - IosmedaView Answer on Stackoverflow
Solution 2 - IosSuragchView Answer on Stackoverflow
Solution 3 - IosskwashuaView Answer on Stackoverflow
Solution 4 - IosMayank JainView Answer on Stackoverflow
Solution 5 - IosVineesh TPView Answer on Stackoverflow
Solution 6 - IosCalin ChituView Answer on Stackoverflow
Solution 7 - IosPunjabiCoderView Answer on Stackoverflow
Solution 8 - IosBhanuView Answer on Stackoverflow
Solution 9 - IosyonlauView Answer on Stackoverflow
Solution 10 - IosJarvanView Answer on Stackoverflow