How to make a push-sedge when choosing a UITableViewCell

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

The question is, is it possible to programmatically create a push sega? If not, can I pass the effect through prepareforsegue ? If I try to use prepareforsegue , I prepareforsegue into a problem where dragging a control from a UITableView does not allow me to create a push sega in the effects view controller.

+49
ios objective-c uitableview segue
Mar 31 '14 at 10:25
source share
9 answers

control drag from view controller to view controller

From View Controller to View Controlle!

You need to specify the identifier of your segment:

enter image description here

Run the segue command:

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

Now here's what, it will just execute segue if you ever need to pass some data to this controller. Then you should 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 } } 
+162
Mar 31 '14 at 12:31
source share

Here is another option that does not require the use of didSelectRowAtIndexPath.

You simply connect 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 } } } 
+37
Aug 13 '15 at 18:43
source share

Swift

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

Here's how to set up a project.

  • Create a project with a TableView . See here for a simple example.
  • Add a second ViewController .
  • Control drag and drop from the first view controller with a table view to the second view controller. Select "Show" as the type of segment.

enter image description here

  • Click the segue button in the storyboard, and then in the Attributes Inspector enter 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 everything in the navigation controller by selecting the first view controller in the storyboard and choosing Editor> Paste in> Navigation Controller .

The code

The first view of the 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 simpler example on transferring data between view managers, see this answer .

+29
Aug 11 '15 at 8:06
source share
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"YourPushSegueName" sender:self]; } 
+13
Mar 31 '14 at 10:33
source share
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MyViewController *myVController = (MyViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"StoryBordIdentifier"]; [self.navigationController pushViewController:myVController animated:YES]; } 
+5
Mar 31 '14 at 10:34
source share

You have 2 options: ctrl + drag from the prototype cell to the new vc, and then you will go from each cell, and you will need to prepare for segue.

The second option, in didselectrowatindexpath you can create an instance of your new view controller using self.storyboard instantiateviewcontrollerwithidentifier ..., then call pushviewcontroller or presentviewcontroller, you do not need segues in the storyboard or prepare for segue in this scenario.

+3
Mar 31 '14 at 10:30
source share

You can use this method:

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

You can perform the function of selecting a specific table cell.

+2
Mar 31 '14 at 10:30
source share

You can do this by following these methods:

First, you need to import another view controller into the current view.

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

inside the method above you must call the method - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; in the following way:

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

This way you can reach the push segment by selecting UITableViewCell .

Hope this helps you ...!

0
Mar 31 '14 at 10:48
source share

Swift 3

After Ahmed Daou answer → manually drag segue from vc to vc to the 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 } } 
0
Dec 14 '16 at 6:28
source share



All Articles