11

Sorry for the question without any line of code. I need advice how to proceed, if at possible that I want.

In Swift language.

Let us assume there is an app with two controllers - UITableViewController with embedded NavigationController, as the main. When you select a row in the table, opens UIViewController, which displays detailed information about the selected value.

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

Once again I apologize and thank you in advance!

Jack
  • 16,677
  • 8
  • 47
  • 51
Alexey Nakhimov
  • 2,673
  • 8
  • 34
  • 49

2 Answers2

43

Yes you can do this in swift.Just load your tableViewController as usual.You just have 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 will not trigger delegate methods like didSelectRowAtIndexPath: so you have to manually trigger this delegate method

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

Or If you are pushing ViewControllers with segue you have to trigger performSegueWithIdentifier

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

Now you can push viewController in didSelectRowAtIndexpath.To select the first row in first section or execute whaterver you have written in didSelectRowAtIndexpath: of tableView

As For your case just push the viewController on select at 0th index with 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 the view controller pushed and if you do not want to animate view controller just pass NO to pushViewController method.

On pressing Back button you will back 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)
}
codester
  • 36,891
  • 10
  • 74
  • 72
  • In my viewDidAppear: `........ if firstStart { firstStart = false let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None) self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect) }` my **didSelectRowAtIndexPath**: `override func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) { self.performSegueWithIdentifier("showForecastSelectedCity", sender: self) }` – Alexey Nakhimov Jul 17 '14 at 05:39
  • Error: `[_TtC12WeatherTest229FavWeatherTableViewController tableView:didSelectRowAtIndexPath:]: unrecognized selector sent to instance 0x7f7fea81d880 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC12WeatherTest229FavWeatherTableViewController tableView:didSelectRowAtIndexPath:]: unrecognized selector sent` What is wrong? – Alexey Nakhimov Jul 17 '14 at 05:41
  • 1
    As you are not implementing `didSelectRowAtIndexPath` you do not need to call `self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect)`. – codester Jul 17 '14 at 07:22
9

You can also do this simply by accessing the tableView delegate to force didSelectRowAtIndexPath to fire.

Programatically select a row to work with:

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

Programatically selecting row does not fire the "didSelectRowAtIndexPath" function so we must manually force the firing as per below:

self.tableView.delegate!.tableView!(tableView, didSelectRowAtIndexPath: indexPath!)

Objective-C:

[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:path];
Boris Y.
  • 4,387
  • 2
  • 32
  • 50
John Carto
  • 165
  • 1
  • 4
  • Thank you so much! It took a little modifying for Swift 4: `tableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.top)` and `tableView.delegate!.tableView!(tableView, didSelectRowAt: indexPath)` – velkoon Dec 09 '18 at 00:25
  • Straight to the point and much cleaner than the selected answer. Works very nicely. – 10000RubyPools Jan 05 '19 at 19:43