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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With