クリック可能なセクションを含むUITableviewを作成しました。それらをクリックすると、
- それらは「拡張」してその中の細胞を明らかにします
- クリックしたセクションがビューの上部にスクロールします。
必要なセルを挿入/削除するためにすべてのインデックスパスを計算してから、次のコードでそれらを挿入します。
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:pathsToOpen withRowAnimation:insertAnimation];
[self.tableView deleteRowsAtIndexPaths:pathsToClose withRowAnimation:deleteAnimation];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:[pathsToOpen objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
問題は1つだけです。選択したセクションの下のセクションが非表示になります。最初のスクリーンショットは、テーブルビューがどのように見えるかを示しています。2番目のスクリーンショットは、実際にどのように見えるかを示しています。
上にスクロールして(非表示のセクションが画面外に表示されるように)、次に下にスクロールすると、非表示のセクションが元に戻ります(もう一度表示されます)。これが起こっている理由についての私の推測は次のとおりです。
挿入/削除アニメーションは同時に発生してscrollToRowAtIndexPath
おり、TableViewを混乱させています。私がscrollToRowAtIndexPath
セクション3と4を実行していなかったとしたら、画面外になっていたでしょう。そのため、tableViewはどういうわけかまだ画面外にあると考えています。UITableviewは、最適化として画面外にあるセル/セクションを非表示にします。2秒scrollToRowAtIndexPath
でaを呼び出すと、セクション3と4が正しく表示されます。dispatch_after
したがって、これが発生している理由はわかっていると思いますが、このUITableviewの最適化を修正/オーバーライドする方法がわかりません。実際、scrollViewDidEndScrollingAnimation
この関数にブレークポイントを実装してから追加すると、アプリはセクション3と4を正しく表示します(これが最初のスクリーンショットを取得した方法です)。しかし、この機能を続けると、細胞は消えます。
プロジェクト全体はここからダウンロードできます
追加の実装の詳細:セクションは正当なUITableViewセクションです。テーブルビューへのデリゲートコールバックをトリガーするtapGestureRecognizerを追加しました。以下に含まれているのは、セクションを開くメソッド全体です。
- (void)sectionHeaderView:(SectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened
{
// Open
sectionHeaderView.numRows = DefaultNumRows;
sectionHeaderView.selected = YES;
NSMutableArray *pathsToOpen = [[NSMutableArray alloc] init];
for (int i = 0; i < sectionHeaderView.numRows; i++)
{
NSIndexPath *pathToOpen = [NSIndexPath indexPathForRow:i inSection:sectionOpened];
[pathsToOpen addObject:pathToOpen];
}
// Close
NSMutableArray *pathsToClose = [[NSMutableArray alloc] init];
if (openSectionHeader)
{
for (int i = 0; i < openSectionHeader.numRows; i++)
{
NSIndexPath *pathToClose = [NSIndexPath indexPathForRow:i inSection:openSectionHeader.section];
[pathsToClose addObject:pathToClose];
}
}
// Set Correct Animation if section's already open
UITableViewRowAnimation insertAnimation = UITableViewRowAnimationBottom;
UITableViewRowAnimation deleteAnimation = UITableViewRowAnimationTop;
if (!openSectionHeader || sectionOpened < openSectionHeader.section)
{
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
openSectionHeader.numRows = 0;
openSectionHeader.selected = NO;
openSectionHeader = sectionHeaderView;
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:pathsToOpen withRowAnimation:insertAnimation];
[self.tableView deleteRowsAtIndexPaths:pathsToClose withRowAnimation:deleteAnimation];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:[pathsToOpen objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}