Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert table view cell in table view while scrolling?

I'm currently working on a stream of data displayed in a UITableView instance. New items are added at the top of the table view.

The problem I'm facing is that a slight interruption of animations and gestures is visible when a new item is added to the table view. The hiccup occurs both when the table view is decelerating after dragging (animation) as well as when the table view is being dragged.

So far, I've tried removing any insertion animations as well as compensating for the insertion by updating the table view's contentOffset. Neither of these approaches seem to work.

like image 785
Bart Jacobs Avatar asked Nov 11 '22 11:11

Bart Jacobs


1 Answers

I just came up with this problem. I was trying to add new rows to a tableView when it reached the last row. You cannot do this whenever you want or you end up with a choppy animation or a sudden stop on the tableView animation.

I figured out that you cannot start a beginAnimations/endAnimations block before the animation (or user drag gesture) ends. To accomplish this, you have to check for two methods:

--(void)scrollViewDidEndDragging:willDecelerate:: This will tell you when a user stops dragging the tableView, possibly at the end of the tableView. If willDecelerate is NO, that is telling you that the user is "holding" its finger on the tableView and has just lifted it. It is a good place to add your rows. You can download the Twitter App to have a look at an example of this. Try to hold your finger at the last row. The App will not update the tweets until you lift it.

-(void)scrollViewDidEndDecelerating: This will tell you the user has touched the table view and is finishing it deceleration, possibly at the end of the table view. It is a good place to update your tableView too.

Have a look at this code example:

-(void)shouldUpdateOnScrollView:(UIScrollView*)scrollView{
    // Check if we are at the end of the tableView
    CGFloat currentOffset = scrollView.contentOffset.y;
    CGFloat maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    // Change 10.0 to adjust the distance from bottom you want to check for
    if (maximumOffset - currentOffset <= 10.0) {
        // Do not throw an update if we are just updating
        if (!_loading &&  !_allDownloaded) {
            _loading = YES;
            // dispatch_async just to not break the current animation
            dispatch_async(dispatch_get_main_queue(), ^{
                [self obtainNextPostsFromBegining:NO];
            });

        }
    }
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"%@ %@",NSStringFromSelector(_cmd),decelerate?@"YES":@"NO");
    if (!decelerate) {
        // the user has just lifted it's finger. Check if we have to update
        [self shouldUpdateOnScrollView:scrollView];
    }

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        NSLog(@"%@",NSStringFromSelector(_cmd));
    // the tableView has just stopped. Check if we have to update
    [self shouldUpdateOnScrollView:scrollView];
}
like image 131
Pablo Romeu Avatar answered Nov 15 '22 06:11

Pablo Romeu