1

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?

4

1 回答 1

1

你应该使用UITableView's message

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

或者

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

当您插入或删除行时。然后消息

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

会自动调用。

于 2013-07-09T10:59:44.133 回答