Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 877
chrs Avatar asked Aug 05 '13 14:08

chrs


People also ask

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.

How do I scroll to the top of a tableView table?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.


1 Answers

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.

like image 54
Timothy Moose Avatar answered Sep 17 '22 18:09

Timothy Moose