user6820041
user6820041

Reputation: 1213

scrollToRowAtIndexPath throwing error

UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: section (3) beyond bounds (1).'

I've got an array of information being displayed at my tableview, and when I hit a button I'm wanting it to scroll down to a certain cell.

Initially I thought I was getting that error because of the way I was getting my index to jump to:

 if let index = peopleListArray.index(where: { $0.UID == currentUser }) {
       print(index)
            let NSIndex = NSIndexPath(index: Int(index) )
            tableView.reloadData()
            print(peopleListArray.count)
            print(NSIndex)
            tableView.scrollToRow(at: NSIndex as IndexPath , at: .top, animated: true )
        }

But then I replaced "let NSINDex... with

 let NSIndex = NSIndexPath(index: 1 )

and it's still throwing the same error.

when I'm printing out my array count and my NSIndex I'm always getting an 8 for the count (which is correct) and I'm getting 3 for the NSINdexPath which is correct.

I could understand the error if the 3 was out of bounds of my array.count but it definitely isn't.

any ideas?

Upvotes: 0

Views: 1996

Answers (2)

virt
virt

Reputation: 27

There are two options that may be wrong here:

  1. you passed a section that is out of bounds, i.e with index less than 0 or bigger than the specified number of sections in

     numberOfSectionsInTableView:(UITableView *)tableView
    

So just try to use 0 for the section number first.

  1. the index of the row is out of bounds for the specified section (they can be different for each section)

Upvotes: -1

tonik12
tonik12

Reputation: 613

It seems the issue you are having is with the section and not with the row. Try to build the index like this:

NSIndexPath(item: index, section: 0)

Note the section is set to 0.

Upvotes: 2

Related Questions