UICollectionViewCell
ファーストレスポンダー ステータスを受け取ることができるの中にテキスト フィールドがあります。セルは現在画面に表示されていませんUISegmentedControl
。このコントロールには 2 つのセグメントがあり、2 番目のセグメントにヒットすると、UICollectionView
. これが発生した後、セルがプログラムで選択され、そのセル内のテキスト フィールドが最初のレスポンダー ステータスを取得し、キーボードが表示されます。
現在起こっていること (セグメント化されたコントロールからの値の変更からのアクション メソッド内) への呼び出し-[UICollectionView selectItemAtIndexPath:animated:scrollPosition:]
がまったくスクロールしていないことです (そして、私はUICollectionViewScrollPositionTop
;を使用しています…None
。リストを手動で下に移動すると、セルは実際に選択されています (その状態では背景色が暗くなります) が、テキスト フィールドにはファーストレスポンダーのステータスがありません。
スクロールの問題を解決するために、リスト内のセルの位置を確認し、セルのコンテンツ オフセットまでスクロールすることができました (scrollRectToVisible
ここでも使用しました)。次に、それを手動で選択します (また、セルのテキスト フィールドがファーストレスポンダー ステータスになる適切なメソッドを起動するようデリゲートに指示します)。
- (void)directionSegmentedControlChanged:(UISegmentedControl *)sender {
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:sender.selectedSegmentIndex];
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
[self.collectionView setContentOffset:attributes.frame.origin animated:YES];
[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self.collectionView.delegate collectionView:self.collectionView didSelectItemAtIndexPath:path];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
BDKCollectionViewCell *cell = (BDKCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
ここでの問題は、表示されているセル-[collectionView:didSelectItemAtIndexPath:]
が nil であることです。これは、メソッドが起動されたときにコレクション ビューの表示セル セットに含まれていないためです。
これを解決する最善の方法は何ですか? スクロール コードを[UIView animateWithDuration:animations:completion:]
ブロック内に放り込み、完了時に最初のレスポンダーを割り当てようとしましたが、この方法でコレクション ビューを手動でアニメーション化すると、スクロールする必要があるセルの読み込みが無視されます。何か案は?
更新: @Esker に感謝します。彼は、Grand Central Dispatch を使用して少し遅れて「フォーカス選択」アクションを実行することを提案しました。私のソリューションは最終的にこのようになりました。
- (void)directionSegmentedControlChanged:(UISegmentedControl *)sender {
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:sender.selectedSegmentIndex];
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
[self.collectionView setContentOffset:attributes.frame.origin animated:YES];
dispatch_time_t startAfter = dispatch_time(DISPATCH_TIME_NOW, 0.28 * NSEC_PER_SEC);
dispatch_after(startAfter, dispatch_get_main_queue(), ^{
[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.collectionView didSelectItemAtIndexPath:path];
});
}