Hibernia Hibernia - 2 months ago 37
Swift Question

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?

Answer

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.

Comments