jx ch
jx ch

Reputation: 55

How to let UIScrollView invoke scrollViewDidScroll, when contentSize is decreased

I have a dynamic UITableView, which will insert and delete rows when clicking button.

-(IBAction)foldingClicked:(id)sender
{
    self.isInfoFolded = !self.isInfoFolded;
    if( self.isInfoFolded)
    {
       //self.mainTable remove rows

    }else
    {
       //self.mainTable insert rows
    }    
}

and I did some animation in scrollViewDidScroll:

-(void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    CGFloat main_offset = scrollView.contentOffset.y;    
    //animation    
    CGRect image_frame = self.imageView.frame;
    if((main_offset >= 0 ) && (main_offset <= self.imageView.frame.size.height))
    {
        image_frame.origin.y = main_offset/2;
    }else
    {
        image_frame.origin.y = image_frame.size.height/2;
    }
}

It mostly works fine. But when I

  1. click button to insert rows. ( the tableView contentSize increase )
  2. Scroll table view to a big offset.
  3. click button to delete rows. ( the tableView contentSize decrease )

Result: The table view will scroll up because the decreased contentSize is making the valid contentOffset smaller too, during which process my scrollViewDidScroll method is not invoked, that makes the animation incomplete, and leaving imageView at wrong position in my case.

I tried calling scrollViewDidScroll: right after removing rows. However scrollViewDidScroll: is invoked only once and the contentOffset is '0' in that execute. It's not gradually down to '0' . So is there any way for making scrollViewDidScroll being called continuously like it usually do?

Upvotes: 1

Views: 1194

Answers (1)

Andrei Chevozerov
Andrei Chevozerov

Reputation: 1029

You should use UITableView's message

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

or

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

When you're inserting or deleting rows. Then the message

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

will be called automatically.

Upvotes: 1

Related Questions