Scrolling a table with setCurrentPageFirstItemId

I’m having problems scrolling to the currently selected line in my table. When I add a new line to the bottom of the table I use table.size() as the itemId. When I want to scroll I do :

table.setValue(i);
if ((i < table.getCurrentPageFirstItemIndex())
|| (i > table.getCurrentPageFirstItemIndex() + table.getPageLength())) {
table.setCurrentPageFirstItemId(i);
} 

This works most of the times, but sometimes the row is properly selected but the table doesn’t scroll. In those cases getCurrentPageFirstItemIndex gives me i which is the correct value. So the table thinks it has scrolled but it hasn’t.
I also tried

table.setCurrentPageFirstItemId(table.getValue());

and also

table.setCurrentPageFirstItemIndex(i);

both of which should work because the item id is the same as the index, but both of them do the same thing. They work most of the times but sometimes the table doesn’t scroll despite table.getCurrentPageFirstItemIndex() giving the correct value.