Objective c 触摸UITableView时将部分滚动到顶部

Objective c 触摸UITableView时将部分滚动到顶部,objective-c,uitableview,Objective C,Uitableview,我有一个包含多个部分的表视图。触摸节标题时,它会展开: 我想要的是触摸部分标题以滚动到表格视图的顶部: 这是我尝试过的代码。展开/折叠工作正常,但该部分不会滚动到顶部。我想象scrollRectToVisible:sectionRect只是使节在视图中可见。如何让它滚动到顶部 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

我有一个包含多个部分的表视图。触摸节标题时,它会展开:

我想要的是触摸部分标题以滚动到表格视图的顶部:

这是我尝试过的代码。展开/折叠工作正常,但该部分不会滚动到顶部。我想象scrollRectToVisible:sectionRect只是使节在视图中可见。如何让它滚动到顶部

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

    ... adding the content of the header

    // To make header touchable
    header.tag = section;
    UITapGestureRecognizer *headerTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTouched:)];
    [header addGestureRecognizer:headerTapGesture];
}

- (void)sectionHeaderTouched:(UITapGestureRecognizer *)sender {

    ... all the code to handle the expanding / collapsing
    
    // Scroll section to top
    CGRect sectionRect = [standardsTableView rectForSection:section];
    sectionRect.size.height = standardsTableView.frame.size.height;
    [standardsTableView scrollRectToVisible:sectionRect animated:YES];
}

也许
ScrollToRowatineXpath:scrollPosition:animated
会起作用

为要滚动到的行和目标节传入NSNotFound索引路径:

// section 6 just an example here, replace with the target section instead
NSIndexPath *topOfSection = [NSIndexPath indexPathForRow:NSNotFound inSection:6];
[self.tableView scrollToRowAtIndexPath:topOfSection atScrollPosition:UITableViewScrollPositionTop animated:YES];

太好了。这工作做得很好。谢谢