UIScrollView retrieve data from the server one page at a time
I have a UIScrollView which scrolls horizontally, I have lot of data to be displayed on that UIScrollView, when i create the uiscrollview I know exactly the size of the content that I would like to display so I create the frame accordingly.
I get the data from the server that i have to populate in the uiscrollview. 开发者_开发技巧I would like to retrieve one page of data at a time from the server, also I would like to retrieve the second page only when user has scrolled through the end of the first page, that way I will avoid pulling unnecessary data from the server.
Any suggestions on how to do this?
You should use the protocoled method - (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == _currentOffsetHeight)
{
// Do what you want
}
}
See: iOS Developer Library: PageControl Sample Code
When you detect scrolls via the delegate methods, you can start loading data for the next views into memory. Also, be sure to keep the previously viewed view in memory. So for example (if the 5th view is on the screen):
0: not in memory 1: not in memory 2: not in memory 3: not in memory 4: in memory 5: in memory 6: in memory 7: not in memory 8: not in memory
Once you detect the user has arrived at page 5, for example, you start loading the next page. You might even want to consider loading +2 pages ahead, in case the scrolling occurs faster, or the user scrolls past a view. The scrollview also bounces, so if you're missing content on one side it might look bad.
Good luck!
精彩评论