1

UITableViewCellsにシングルタップおよびダブルタップのジェスチャ認識機能を追加しました。しかし、テーブルを数回スクロールした後、テーブルをスクロールするためのスワイプジェスチャが終了してから、スクロールアニメーションが開始されるまでの休止時間がますます長くなります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"tableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    [cell addGestureRecognizer:singleTap];

    UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [cell addGestureRecognizer:doubleTap];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        // search results
    }
    else {
        // normal table
    }

    return cell;
}

SingleTap:およびdoubleTap:メソッド

- (void)singleTap:(UITapGestureRecognizer *)tap
{
    if (UIGestureRecognizerStateEnded == tap.state) {
        UITableViewCell *cell = (UITableViewCell *)tap.view;
        UITableView *tableView = (UITableView *)cell.superview;
        NSIndexPath* indexPath = [tableView indexPathForCell:cell];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        // do single tap
    }
}

- (void)doubleTap:(UITapGestureRecognizer *)tap
{
    if (UIGestureRecognizerStateEnded == tap.state) {
        UITableViewCell *cell = (UITableViewCell *)tap.view;
        UITableView *tableView = (UITableView *)cell.superview;
        NSIndexPath* indexPath = [tableView indexPathForCell:cell];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        // do double tap
    }
}

最初のスクロールはスムーズなので、ジェスチャー認識機能をif (cell == nil)条件に追加しようとしましたが、セルに追加されることはありませんでした。

また、最初は個々のセルではなくジェスチャをtableViewに追加しましたが、これによりsearchDisplayControllerで問題が発生しました。つまり、キャンセルボタンをタップしても認識されません。

どんな考えでも感謝します、ありがとう。

4

1 に答える 1

6

cellForRowAtIndexPathメソッドは、同じNSIndexPathに対して複数回呼び出されるため、セルに追加するジェスチャ認識機能が多すぎます。したがって、パフォーマンスが低下します。

私の最初の提案は、テーブルビューにジェスチャレコグナイザーを1つだけ追加することです。(私は同様の質問のためにこの答えを書きました: https://stackoverflow.com/a/4604667/550177 )

しかし、あなたが言ったように、それはsearchDisplayControllerで問題を引き起こします。たぶん、UIGestureRecognizerDelegateのスマートな実装でそれらを回避することができます(-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;セル内ではなくタップが発生した場合はNOを返します)。

私の2番目の提案:ジェスチャ認識機能を1回だけ追加します。

if ([cell.gestureRecognizers count] == 0) {
    // add recognizer for single tap + double tap
}
于 2012-03-12T22:58:50.193 に答える