简体   繁体   English

UITableView滚动到部分

[英]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 . 使用scrollRectToVisible而不是scrollToRowAtIndexPath 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 . 因此,如果您希望将标题精确定位在顶部,请使用标题的框架,将高度设置为表视图的高度,然后将其传递给scrollRectToVisible

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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