iphoneuitableviewuiscrollviewscrollsectionheader

Iphone : How to scroll to the 1st cell of the 2nd section, letting the header of the 1st section visible


I have an UITableView with rows and sections. I would like to scroll to the first item of the second section, letting the header of the first section visible. Like if I had manually scrolled the list until reaching that state.

---- TOP OF SCREEN ----
Header of first section
Header of the second section
cell 1
cell 2
cell 3
Header of the third section
cell 1
cell 2
...

scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] does not do the job, it hides the header of the first section.


Solution

  • We're moving on. I found this method based on Kevin's idea. To be able to set animated to YES, I catch the end of animation using a delegate method of UIScrollView. It works. But any solution that would help not doing 2 animations would be greatly appreciated. Any idea about how to do this ?

    - (IBAction) scrollToToday:(BOOL)animate {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
        if (animate == NO) [self showFirstHeaderLine:NO];
    }
    
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
        [self showFirstHeaderLine:YES];
    }
    
    - (void) showFirstHeaderLine:(BOOL)animate {
        CGRect headerRect = [self.tableView rectForHeaderInSection:1];
        CGPoint scrollPoint = headerRect.origin;
        scrollPoint.y -= headerRect.size.height;
        [self.tableView setContentOffset:scrollPoint animated:animate];
    }
    

    Dude to this code, the process when animated is set to YES should loop infinitely beetween scrollViewDidEndScrollingAnimation and showFirstHeaderLine... It loops, yes, but only once... Any idea about why ?