呼び出し後-[UICollectionView reloadData]
、セルが表示されるまでに時間がかかるため、呼び出し直後の項目の選択reloadData
はできません。直後にアイテムを選択する方法はありますreloadData
か?
9 に答える
この回答に沿って、collectionViewに対して他のことを行う前に、 layoutIfNeeded
afterを呼び出すとreloadData
リロードが効果的に「フラッシュ」されるように見えることがわかりました。
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
...
私がこの解決策を見つけたページで、一部のコメント投稿者は、iOS 9では機能しないと述べましたが、私にとっては問題ないので、マイレージは異なる場合があります。
でセルの選択を処理していcollectionView: cellForItemAtIndexPath:
ます。私が見つけた問題は、セルが存在しない場合、単に呼び出すだけでselectItemAtIndexPath: animated: scrollPosition:
は実際にはアイテムが選択されないということでした。
代わりに、次のことを行う必要があります。
cell.selected = YES;
[m_collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
reloadDataを使用しないでください
- (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion
代わりに使用してください。完了ブロックは、セルの挿入/削除などのアニメーションが完了した後に実行されます。(void (^)(void))updates
ブロックにreloadDataの呼び出しを置くことができます
Appleは言う:
アイテムが挿入または削除されるアニメーションブロックの途中でこのメソッドを呼び出さないでください。挿入と削除により、テーブルのデータが自動的に適切に更新されます。
UICollectionView
実際、アニメーションの途中(スクロールを含む)でこのメソッドを呼び出さないでください。
したがって、次を使用できます。
[self.collectionView setContentOffset:CGPointZero animated:NO];
[self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
または、アニメーションがないことを確認してから、reloadData
;を呼び出します。また
[self.collectionView performBatchUpdates:^{
//insert, delete, reload, or move operations
} completion:nil];
これがお役に立てば幸いです。
スウィフトウェイ:
let selected = collectionView.indexPathsForSelectedItems()
collectionView.performBatchUpdates({ [weak self] in
self?.collectionView.reloadSections(NSIndexSet(index: 0))
}) { completed -> Void in
selected?.forEach { [weak self] indexPath in
self?.collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: [])
}
}
reloadData
メインスレッドを呼び出していることを確認してください。これが、セルの更新の遅延の原因である可能性があります。
willDisplayCellcolelctionViewデリゲートで処理しました。アイデア:最初のスクロールがすでに実行されているかどうかを指定するには、一時変数が必要です(scrollIsRequired)。最後に表示されたセルが表示されたら、必要なセルまでスクロールしてこの変数を設定し、再度スクロールしないようにします。
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
//Perform any configuration
if (CGRectGetMaxX(collectionView.frame) <= CGRectGetMaxX(cell.frame)) {
// Last visible cell
if (self.scrollIsRequired) {
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.initiallySelectedRepresentativeVerse inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
self.scrollIsRequired = NO;
}
}
}
それは私にとって魅力のように働いてきました。
これは私のために働いたものです:
選択したインデックスパスの参照を保持し、reloadData関数をオーバーライドしました。
override func reloadData() {
super.reloadData()
self.selectItem(at: self.selectedIndexPath, animated: false, scrollPosition: UICollectionViewScrollPosition())
}
I tried doing it using indexPathForSelectedItems, but it was creating an infinite loop on collection view load.
選択を行うメソッドを作成し、reloadを呼び出した後にperformSelectorを使用して呼び出します。
[self performSelector:@selector(selectIt) withObject:self afterDelay:0.1];