Programmatically emulate a selection in a UITableViewController in Swift

Sorry for the question without any line of code. I need advice on how to act, if possible, what I want.

In the Swift language.

Suppose the application has an application with two controllers - a UITableViewController with a built-in NavigationController . When you select a row in the table, open the UIViewController , which displays detailed information about the selected value.

Is it possible to do otherwise? When the application starts, programmatically emulate the selection of the first row in the table and immediately display the UIViewController with detailed information about the selected value. And from this controller, you can return to the UITableViewController through the NavigationController .

I apologize again and thank you in advance!

+8
ios uitableview swift
source share
2 answers

Yes, you can do it fast. Just load tableViewController as usual. You just need to call

In swift

  let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0); //slecting 0th row with 0th section self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None); 

The selectRowAtIndexPath function selectRowAtIndexPath not run the delegate methods, such as didSelectRowAtIndexPath: so you need to manually run this delegate method

  self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select 

Or If you click ViewControllers using segue, you need to call performSegueWithIdentifier

  self.performSegueWithIdentifier("yourSegueIdentifier", sender: self); 

Now you can click viewController in didSelectRowAtIndexpath . To select the first row in the first section or to execute the whaterver that you wrote in didSelectRowAtIndexPath: from tableView

As for your case, just click viewController to select in the 0th index using didSelectRowAtIndexpath

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){ if indexPath.row == 0 { self.navigationController.pushViewController(yourViewControllerToPush, animated: YES); } //handle other index or whatever according to your conditions } 

It will show that the view controller is pressed, and if you do not want the animation view controller to just go through the NO method to pushViewController .

When you click the Back button, you will return to your viewController

In your case, just write this in your viewDidAppear

 if firstStart { firstStart = false let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition:UITableViewScrollPosition.None) self.performSegueWithIdentifier("showForecastSelectedCity", sender: self) } 
+36
source share

You can also do this simply by contacting the tableView delegate to make "didSelectRowAtIndex" run

// programmatically select the line to work with

 self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.Top) 

// Programmatically selecting a line does not start the function "didSelectRowAtIndexPath", so we must manually force the shooting, as shown below.

 self.tableView.delegate!.tableView!(tableView, didSelectRowAtIndexPath: indexPath!) 
+4
source share

All Articles