2

行とセクションを持つ UITableView があります。最初のセクションのヘッダーが表示されるように、2 番目のセクションの最初の項目までスクロールしたいと思います。その状態に達するまでリストを手動でスクロールしたかのように。

---- 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

進んでいます。この方法は、Kevin のアイデアに基づいて見つけました。アニメーションを 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デリゲートは呼び出されません。SO scrollViewDidEndScrollingAnimation は、scrollToRowAtIndexPath から呼び出された 1 回だけ呼び出されます。

于 2015-08-12T05:11:27.547 に答える