2

I am trying to programmatically select a cell in a tableview. I get a SIGABORT error when hitting the selectRowAtIndexPath. myIndex has a value of 1. I am calling this from a viewController that has implemented the required tableview delegate and datasource methods.

override func viewDidAppear(animated: Bool) {
    if myIndex != nil {
        self.myTableView.selectRowAtIndexPath(NSIndexPath(index: myIndex), animated: false, scrollPosition: UITableViewScrollPosition.Middle)
    }
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
branimal
  • 97
  • 9

2 Answers2

3

Pay attention to error messages in console - they are very helpful most of the times. When you run your code you'll see message:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.

Which describes both what the problem is and how to fix it - you should use different initializer for NSIndexPath

self.myTableView.selectRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0), animated: false, scrollPosition: .Middle)
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • *Excellent* answer! I was about to post something similar, but I could not have explained it any better or more completely. (Voted.) – Duncan C Feb 29 '16 at 20:35
  • Thanks. I just figured out how to show the ERROR console. Is there a good tutorial on debugging and / or reading the error console? Thanks – branimal Feb 29 '16 at 21:34
  • Sorry, can't point to good tutorials, may be this one will help - http://www.raywenderlich.com/28289/debugging-ios-apps-in-xcode-4-5. You can also watch wwdc videos on debugging from previous years, there's tons of great insights in them – Vladimir Feb 29 '16 at 21:48
2

selectRowAtIndexPath will want an indexPath with row and section. Not just an index.

gnasher729
  • 51,477
  • 5
  • 75
  • 98