Iphone:如何滚动到第二部分的第一个单元格,让第一部分的标题可见

Iphone:如何滚动到第二部分的第一个单元格,让第一部分的标题可见,iphone,uitableview,uiscrollview,scroll,sectionheader,Iphone,Uitableview,Uiscrollview,Scroll,Sectionheader,我有一个包含行和节的UITableView。 我想滚动到第二节的第一项,让第一节的标题可见。就像我手动滚动列表直到达到那种状态 ---- TOP OF SCREEN ---- Header of first section Header of the second section cell 1 cell 2 cell 3 Header of the third section cell 1 cell 2 ... ScrollToRowatineXpath:[NSIndexPath indexP

我有一个包含行和节的UITableView。 我想滚动到第二节的第一项,让第一节的标题可见。就像我手动滚动列表直到达到那种状态

---- TOP OF SCREEN ----
Header of first section
Header of the second section
cell 1
cell 2
cell 3
Header of the third section
cell 1
cell 2
...
ScrollToRowatineXpath:[NSIndexPath indexPathForRow:0第1节]
不执行此操作,它会隐藏第一节的标题。

您可以获取所需行的矩形,然后减去上一节标题的高度并滚动到该点。类似于以下(未经测试)的方法应该可以工作:

CGRect rowRect = [table rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
CGRect headerRect = [table rectForHeaderInSection:0];
rowRect.origin.y -= headerRect.size.height;
rowRect.size.height += headerRect.size.height;
[table scrollRectToVisible:rowRect animated:YES]; // UITableView is a subclass of UIScrollView

我们继续前进。我根据凯文的想法找到了这个方法。为了能够将动画设置为“是”,我使用UIScrollView的委托方法捕捉动画的结尾。它起作用了。但任何有助于不制作2个动画的解决方案都将不胜感激你知道怎么做吗?

- (IBAction) scrollToToday:(BOOL)animate {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
    if (animate == NO) [self showFirstHeaderLine:NO];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self showFirstHeaderLine:YES];
}

- (void) showFirstHeaderLine:(BOOL)animate {
    CGRect headerRect = [self.tableView rectForHeaderInSection:1];
    CGPoint scrollPoint = headerRect.origin;
    scrollPoint.y -= headerRect.size.height;
    [self.tableView setContentOffset:scrollPoint animated:animate];
}

根据这段代码,当动画设置为“是”时,该过程应该在ScrollViewDiEndScrollingAnimation和showFirstHeaderLine之间无限循环。。。它循环,是的,但只有一次知道为什么吗?

我试过你的代码,它很管用

- (IBAction) scrollToToday:(BOOL)animate {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
    if (animate == NO) [self showFirstHeaderLine:NO];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self showFirstHeaderLine:YES];
}

- (void) showFirstHeaderLine:(BOOL)animate {
    CGRect headerRect = [self.tableView rectForHeaderInSection:1];
    CGPoint scrollPoint = headerRect.origin;
    scrollPoint.y -= headerRect.size.height;
    [self.tableView setContentOffset:scrollPoint animated:animate];
}

对于循环问题,因为您正在设置偏移量(SetContentOffset),所以它与滚动无关。它不会调用scrollView委托。因此,ScrollViewDiEndScrollingAnimation将只调用一次,这是从ScrollToRowatineXpath调用的。

这是一个好主意,但这不起作用。事实上,它没有做任何事情,可能是因为行已经可见。但是保留这个想法,是否可以让事情发生,然后向下滚动一点以显示隐藏的标题?啊,是的,如果它已经可见,这将不会起任何作用。你所能做的就是计算原点Y坐标(
rowRect.origin.Y-headerRect.size.height
),然后用它来设置
contentOffset
属性(你需要使用
-setContentOffset:animated:
来设置动画)。这类事情最好是对原始问题进行编辑。但是。。。如果我用解决方案编辑问题,这将不再是问题:-)