简体   繁体   中英

UITableView Scroll to section

Is it possible to scroll to a section instead of a row? If so how?

Btw. I am using a method to remove the floating headers.

Here is the code that I use to move to the selected sections first row.

if (self.openSectionIndex != NSNotFound) {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

this is the code to remove the floating headers

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.openSectionIndex != NSNotFound) {
        if (scrollView.contentOffset.y<=DEFAULT_HEADER_HEIGHT&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=DEFAULT_HEADER_HEIGHT) {
            scrollView.contentInset = UIEdgeInsetsMake(-DEFAULT_HEADER_HEIGHT, 0, 0, 0);
        }
    }
}

This is somehow what I want but I would rather also show the header.

Right now the header is hidden offscreen in the top.

Instead of scrollToRowAtIndexPath , use scrollRectToVisible . You can get precise positioning by passing in a rect with a size that is the same as the table view.

So if you want your header to be positioned exactly at the top, use the header's frame, setting the height to that of the table view, and then pass that to scrollRectToVisible .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM