The solution is to go backwards. I do not have a table view to play with to write some code for you.
I do not have the problem.
A list (table) with 10 row have the indexes 0,1,2,3,4,5,6,7,8 and 9
If you iterate through the list
0,1,2,3,4 and then you remove 4 the list will shrink. BUT the iteration will still run till the specified endindex (9) but the list does not contain such items.
If you to from 9 to 0 step -1
9,8,7,6,5,4 (you now remove 4),3,2,1,0