iPhone:2番目のセクションの第1セルにスクロールする方法。1番目のセクションのヘッダーを表示する

StackOverflow https://stackoverflow.com/questions/4597839

質問

行とセクションを備えた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]は仕事をしておらず、最初のセクションのヘッダーを隠します。

役に立ちましたか?

解決

先に進んでいます。ケビンのアイデアに基づいてこの方法を見つけました。アニメーションを「はい」に設定できるようにするために、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];
}

このコードのおい、アニメーション化されたときのプロセスは、scrollviewdidendscrollinganimationとshowfirstheaderlineの間で無限にループする必要があります...それはループ、はい、しかし一度だけ... 理由について何か考えがありますか?

他のヒント

必要な行の長方をつかみ、前のセクションのヘッダーの高さを差し引いて、そのポイントまでスクロールできます。次の(テストされていない)のようなものは機能するはずです。

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

私はあなたのコードを試しました、そしてそれは動作します!!

ループの質問については、オフセット(setContentOffset)を設定しているため、スクロールとは何の関係もありません。 Scrollview Delegateを呼び出しません。したがって、ScrollViewDidendScrollinganimationは、Scrolltorowatindexpathから呼び出された1回だけ呼び出されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top