0

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!

freshking
  • 1,824
  • 18
  • 31
  • The problem is tied to scrollViewDidScroll, which fires, as the name implies, whenever it scrolls. Coupled with the fact that your condition for inserting a new row depends on some offset value you basically enter a state where you can and do fire multiple times. – timothykc Jul 01 '14 at 15:40
  • There are other ways to detect when you get to the bottom, for example: http://stackoverflow.com/questions/5137943/how-to-know-when-uitableview-did-scroll-to-bottom-in-iphone – timothykc Jul 01 '14 at 15:44

0 Answers0