Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

iOS: scrollToRowAtIndexPath doesn't display correct top indexpath for last page

I am trying to scroll tableview programmatically as coded following. Please note that I have increased the content size so I can see few last few remaining rows. I can scroll manually correctly to get required indexpath at top but programmatically it's not happening.

[tableview reloadData];

CGSize size = tableview.contentSize;
size.height += 1000;
tableview.contentSize = size;

int rows = [tableview numberOfRowsInSection:0];
int numberofRowsInView = 17;
for (int i = 0; i < ceil((float)rows/numberofRowsInView); i++) {
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i * numberofRowsInView inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
For total rows 60, I expect this code to return cell views of 0-16, 17-33, 34-50, 51-59 but for the last scroll it returns 43-59 i.e. full 17 rows view while based on above code, top indexpath should be 51 for the last page!

Can someone please help me to sort this out. Thanks.


  • dev666999dev666999 Posts: 3,632New Users @ @ @ @ @
    edited July 2013
    "ceil" rounds up to the nearest integer.

    Insert this inside and at the beginning of your FOR make it the first line inside the loop.

    if(i == 3)
    numberofRowsInView = 9;

    You can also use...

    if(ceil((float)rows/numberofRowsInView) == 3)
    numberofRowsInView = 9;

    The above two may not be the best way to do it, but it gets the job done.
Sign In or Register to comment.