0

I am trying to select the first row in the UITableViewController once it appears (through a segue).

I am using the following code which I inherited from my previous Objective C project:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! IGANavlistTableCustomViewCell

        // Code that sets the custom view cell

        // Then...
        // Selecting the first row by default
        if(indexPath.row == 0)
        {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)

            tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None)

            tableView.delegate?.tableView!(tableView, didSelectRowAtIndexPath: rowToSelect)
        }

        return customCell;
    }

I receive the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IGANavlistTableVC tableView:didSelectRowAtIndexPath:]: unrecognized selector sent to instance 0x79e21040'

I would appreciate if someone could help.

Thank you!

EDIT:

This is my didDeselectRowAtIndexPath method:

var navaidSelected = [String]()
var listOfNavPoints = [[String]]()

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
    {
        // The selected object of the Array with all navaids data (i.e., the selected navaid) is converted to a string
        self.navaidSelected = self.listOfNavPoints[indexPath.row]
    }
Igor Tupitsyn
  • 1,193
  • 3
  • 18
  • 45

1 Answers1

4

You forgot implement func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) method.

And you needn't to use delegate when call didSelectRowAtIndexPath:

if(indexPath.row == 0)
        {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)

            tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None)

           self.tableView(tableView, didSelectRowAtIndexPath: rowToSelect)
        }

=== EDITED ===

Change your didDeselectRowAtIndexPath to didSelectRowAtIndexPath.

t4nhpt
  • 5,264
  • 4
  • 34
  • 43
  • 1
    Thanks. Sorry, I did not include it in the original message. Corrected. Also, having it your way gives me the following error: "Cannot call value of non-function type 'UITableView'". – Igor Tupitsyn Oct 20 '15 at 02:50
  • Thank you. Changed. No compilation error, but still the runtime error. Interesting that if I select the second row and then the first row and select it, no error occurs. – Igor Tupitsyn Oct 20 '15 at 03:13
  • The excited question about this here:http://stackoverflow.com/questions/2106292/uitableview-didselectrowatindexpath-not-being-called-on-first-tap – t4nhpt Oct 20 '15 at 03:30
  • Thank you so much! I thought I was going crazy. Three days of trying to find the error because of two extra letters! Appreciate your help! – Igor Tupitsyn Oct 20 '15 at 10:17