2

インターフェイスのような Twitter iPhone アプリを実装しようとしています。(スワイプして、tableviewcell のビューをカスタム ビューに置き換えます)。スワイプを認識するためにApple を使用しUISwipeGestureRecognizerており、 を使用してそのスワイプの開始位置を取得してい[recognizer locationInView:self.view]ます。これにより CGPoint が得られ、それを で使用してい[tableView indexPathForRowAtPoint:location]ます。これに関する私の問題は、スワイプが常に、スワイプした実際の行の 1 行上または下で検出されるように見えることです。同じ問題が発生した人はいますか?

編集:私はおそらく、カスタム テーブルビュー セルを使用していることにも言及する必要があります。その高さは、デフォルトのビューよりも大きくなっています。それが違いを生むかどうかはわかりませんがheightForRowIndexAtPath:、高さを返すために使用しています。

私のコードは -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelObject *rowData = (ModelObject *)[tempArr objectAtIndex:indexPath.row];
if (rowData.isAlternateView) {
        // Load alternateview
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [cell addGestureRecognizer:recognizer];
    [recognizer release]; 
    return cell;

}
else {
    // Load the correct uitableviewcell
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [cell addGestureRecognizer:recognizer];
    [recognizer release]; 

    return cell;
}

}


-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
NSLog(@"Swipe detected!");
CGPoint location = [recognizer locationInView:self.view];   
NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];
NSLog(@"Swipe detected at %d", selectedIndexPath.row);
ModelObject *rowData = (ModelObject *)[modelArr objectAtIndex:selectedIndexPath.row];
rowData.isAlternateView = YES;

for (int i=0; i<[tempArr count]; i++) {
    if (i!=selectedIndexPath.row) {
        ModelObject *rowToBeCleared = (ModelObject *) [modelArr objectAtIndex:i];
        if ([rowToBeCleared isAlternateView]) {
            [rowToBeCleared setIsAlternateView:NO];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:0],nil] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:selectedIndexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
}
4

1 に答える 1

10

まず、各セルにジェスチャ認識エンジンを追加するのではなく、テーブル ビューを含むビュー全体にジェスチャ認識エンジンを 1 つだけ追加することをお勧めします (または、これが UITableViewController の場合、それらは同じものです)。

ビューでDidLoad:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

次に、handleSwipe メソッドで:

- (void)handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
    //might need to check if swipe has ended (otherwise not time to handle yet)
    if (recognizer.state != UIGestureRecognizerStateEnded)
        return;

    CGPoint location = [recognizer locationInView:tableView]; //not self.view  
    NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];

    if (selectedIndexPath != nil)
    {
        //user swiped on a tableview cell
    }
    else
    {
        //user did not swipe on a tableview cell
    }
}
于 2010-11-10T19:56:14.483 に答える