11

補足ビューを備えた UICollectionView があります。本質的には、コレクションのヘッダーです。インターフェイス ビルダーを使用して、headerView.xib 内の UILabel にジェスチャ レコグナイザーを追加すると、アプリがクラッシュします。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (MY_HEADER) - nib must contain exactly one top level object which must be a UICollectionReusableView instance'

UICollectionView の補足ビュー内の UILabel にジェスチャ レコグナイザーを追加できない原因は何ですか?

4

4 に答える 4

20

そのため、UICollectionView の補足ビューにジェスチャ認識機能を追加するためにインターフェイス ビルダーを使用 できないようです。

これは、.xib が読み込まれると、UICollectionView がスーパービューに 1 つのものとして表示される必要があるためだと思います。また、その UICollectionView にジェスチャ レコグナイザーを追加すると、スーパービュー レベルで 2 つのものになり、どちらもUICollectionView.

ただし、UICollectionViewReusableView プロトコル内の補足ビューの定義を使用して、プログラムでジェスチャ レコグナイザーを実装できます。(if は、コードの後半でヘッダー補足ビューとフッター補足ビューを区別するために使用されています)

if (kind == UICollectionElementKindSectionHeader) {
    MyHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MY_HEADER" forIndexPath:indexPath];

    // call headerView methods (to put things into the header's UI objects for example)
    [headerView ...];
    [headerView ...];
    [headerView ...];

    // add gesture recognition for tapping on a UIlabel within the header (UICollectionView supplementary view)
    UITapGestureRecognizer *bioTap = [[UITapGestureRecognizer alloc] initWithTarget:headerView action:@selector(handleUILabelTap:)];
    // make your gesture recognizer priority
    bioTap.delaysTouchesBegan = YES;
    bioTap.numberOfTapsRequired = 1;
    [headerView.UILabelName addGestureRecognizer:UILabelTap];

    reusableview = headerView;
}
于 2013-10-03T15:16:40.957 に答える
1

私もIB経由でセルにジェスチャを追加できませんでした。

ただし、私の経験では、IB を使用すると、グラフィック表現の collectionView の上にある scrollView ではなく、アウトライン ビューの collectionView アイテムにジェスチャ認識機能をドラッグすることで、collectionView 自体にジェスチャ認識機能を追加できます。

これまでのところ、セルを 1 回タップして collectionView に入ることができます。

于 2014-03-28T21:46:04.783 に答える
0

ペン先がロードされた後にプログラムで追加するのはどうですか? または、IB で、認識機能を表すアイコンの位置を、ビューを表すアイコンの上または下に移動しようとしましたか?

于 2013-10-02T03:59:46.493 に答える