0

ストーリーボードでlongPressGestureを初期化しましたが、のセルに追加したいと思いcollectionViewます。

問題は次のとおりです。ジェスチャーは、最後に追加されたセルでのみ機能します。collectionView

これは私のコードです:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     MyCollectionCell *cell = (MyCollectionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    [cell addGestureRecognizer:longPressGesture];
    return cell;
}

.hファイル:

IBOutlet UILongPressGestureRecognizer *longPressGesture;
4

1 に答える 1

1

ジェスチャターゲットメソッドがすべてのセルに対して同じ操作を行う場合は、UILongPressGesture毎回[新規作成]を実行し、次のUICollectionViewようにセルに割り当てます。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

     MyCollectionCell *cell = (MyCollectionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] 
                                          initWithTarget:self action:@selector(handleGesture:)];
     longPressGesture.minimumPressDuration = 1.5; 

    [showUserMap addGestureRecognizer:lpgr];

    [cell addGestureRecognizer:longPressGesture];
    return cell;
}
于 2013-03-13T13:20:39.350 に答える