2

我有一个带有行和部分的 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
...

scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] 不起作用,它隐藏了第一部分的标题。

4

3 回答 3

4

我们继续前进。我根据凯文的想法找到了这个方法。为了能够将动画设置为 YES,我使用 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];
}

对于这段代码,当动画设置为YES时,应该在scrollViewDidEndScrollingAnimation和showFirstHeaderLine之间无限循环......它循环,是的,但只有一次......知道为什么吗?

于 2011-01-06T01:09:31.653 回答
2

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

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
于 2011-01-05T01:32:33.423 回答
0

我试过你的代码,它有效!

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

于 2015-08-12T05:11:27.547 回答