0

I'm using Parse to create my App. My app works fine till I used the didSelectRowAtIndexPath method, as soon as I used the method to pass the data to another view controller to show detailed information, and when i tapped the 'Load More' row, i get error:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'

I googled it and some say: 'The problem might be in your tableView:didSelectRowAtIndexPath: method. This is called when any of your rows is tapped - including the loadMore cell. You might want to check if the cell that is being tapped is the Load More cell, and in that case return (and let the call to super handle loading more data).'

It's fairly reasonable to me, but this guy didn't give a solution and it was the answer 2 years ago, and I'm now using Swift.

So here's my code and anyone who can help me? Thanks.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var targetObject = objectAtIndexPath(indexPath)!

    gameIdSelected = targetObject["GameId"] as! String

    performSegueWithIdentifier("segue", sender: self)
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
WhiteRice
  • 51
  • 6

1 Answers1

0

Your answer still contains the solution

This is called when any of your rows is tapped - including the loadMore cell

If your array of data contains only 13 items (from 0 to 12), if you are including a loadmore cell it will not work because you'll have 14 cells instead of 13. So if you tap on the loadmore cell it will try to access the 14th item of your array but you have only 13 items, so it's why you have this index error.

Just don't call objectAtIndex if your cell is the loadmore cell. Something like this:

if indexPath.row == 13 {
   //Do loadmore stuff
} else {
   var targetObject = objectAtIndexPath(indexPath)!

   gameIdSelected = targetObject["GameId"] as! String

   performSegueWithIdentifier("segue", sender: self)
}

Of course don't hardcode the loadmore value, but I don't know your datasource name. Usually we are doing with something like this:

if indexPath.row == yourDataSourceArray.count()
Lory Huz
  • 1,557
  • 2
  • 15
  • 23