I am trying to keep the tableVies scroll position when I come back into my screen. I am getting this error:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Attempted to scroll the table view to an out-of-bounds row (7) when there are only 4 rows in section 0.
There should be 10 rows in section 0. I think that the tableView is not fully loaded and that's why the error says there is only 1 row in section 0 because this crash doesn't occur most of the time. Usually it works fine. Variable savedRowMainFave is saved in viewWillDisappear
I put some print statements to show values:
Leaving indexPath = [0, 7]. (this is in the viewWillDisappear(). Prints savedRowMainFave = (dealsTable.indexPathsForVisibleRows?.last) ?? savedRowMainFave)
filteredDeals.count = 10 (also in the viewWillAppear() this is the number of items in the array that populates the tableView called dealsTable)
this is the calculated rows = 10 (also in the viewWillAppear() this prints self.dealsTable.numberOfRows(inSection: savedRowMainFave.section))
This code scrolls to the saved position. The if statement is supposed to protect from crashing but is obviously self.dealsTable.numberOfRows(inSection: savedRowMainFave.section) is not working
if (self.dealsTable.numberOfRows(inSection: savedRowMainFave.section) > savedRowMainFave.row){
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {self.dealsTable.scrollToRow(at: savedRowMainFave, at: .bottom, animated: false)
}
Any suggestions for ways of verifying a valid savedRowMainIndex ( an indexPath) would be greatly appreciated. Thanks