In my UITableView
I use scrollViewDidScroll:
to load more cells if more content is available when the user scrolls to the bottom.
I use NSRange
to determine which infos from a plist are shown in the table and how many cells are already loaded.
So if the users clicks on index 12 in the plist the table will show a cell containing that info. NSRange: {12,1} -> index 12, 1 row
Now the user scrolls to the bottom an a new cell is loaded and displayed. NSRange: {12,2} - index 12, 2 rows, etc.
My problem is that the function which updates the table gets called multiple times. So when the table is supposed to load only one more cell below the range is updated multiple times which causes the number of rows to grow larger than 1.
Is there any way to avoid to function being called multiple times in my code here?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat currentOffset = scrollView.contentOffset.y;
CGFloat maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
CGFloat loadDelta = - 75
if (maximumOffset - currentOffset <= loadDelta &&
_commentsRange.length != [[Content manager] numberOfComments] &&
scrollView.isDragging) // load new cell below
{
// this gets called multiple times:
[_largeContentTableView beginUpdates];
[self addBottomRange];
[_largeContentTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:(_commentsRange.length - 1) inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[_largeContentTableView endUpdates];
return;
}
...
}
- (void)addBottomRange
{
_commentsRange = NSMakeRange(_commentsRange.location, (_commentsRange.length + 1));
}
Thanks a lot!