2011-07-08 10 views
3

私はテーブルビューなぜ私の関数は2回呼び出されますか?

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

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

     //swipe recognition 
     UISwipeGestureRecognizer *g = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)]; 
     [cell addGestureRecognizer:g]; 
     [g release]; 


    } 


    // Configure the cell... 
    [[cell textLabel] setText:[NSString stringWithFormat:@" number %d", indexPath.row]]; 
    return cell; 
} 

のセルに添付スワイプ認識を持っており、スワイプ機能が

- (void)cellWasSwiped:(UIGestureRecognizer *)g { 
    NSLog(@"sunt in cellWasSwiped"); 
    swipeViewController *svc = [[swipeViewController alloc]init]; 
    [self.navigationController pushViewController:svc animated:YES]; 
    [svc release]; 
} 

と私は私のスワイプ機能が2回呼び出され、そこにあるされていることが分かりブレークポイントを導入することです2つの同一のビューコントローラがナビゲーションコントローラにプッシュされました。スワイプ機能を2回呼び出したのはなぜですか?

+0

uはスワイプで一度だけ行われていることを確認してくださいましたか? – Dinakar

+0

また、セルではなくセルのcontentViewにジェスチャを追加してみてください。 – Dinakar

+0

あなたがそれをやっているやり方は、個々のセルごとにジェスチャ認識機能を追加することです。 tableView全体に1つだけ追加してみてください。 – Greg

答えて

4

cellWasSwipedは、UIGestureRecognizer状態の変更時に複数回呼び出すことができます。プロパティstateをチェックする必要があります。ユーザがアクションを完了すると、UIGestureRecognizerStateEndedになります。また、UIGestureRecognizerStateFailedUIGestureRecognizerStateCancelledの状態を確認することも良いことです。

+0

それは動作します!ありがとう! – Alex

1

ジェスチャ認識器を直接セルに追加するのではなく、viewDidLoadのテーブルビューに追加できます。次のようにあなたが影響を受けIndexPathとセルを決定することができます方法 - cellWasSwiped

は:

-(void)cellWasSwiped:(UIGestureRecognizer *)gestureRecognizer { 

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; 
     NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
     UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; 
     // ... 
    } 
} 
+0

「CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];」という行にSIGABRTが表示されます。 – Alex

+0

私はobj-c、申し訳ありません –

+0

をurを使用しない場合、 = [gestureRecognizer locationInView:tableViewオブジェクト名]; – Alex

3

私はテーブルのセルにスワイプして、その同じ特定の問題を抱えていました。私のレコグナイザーメソッドが州のUIGestureRecognizerStateEndedで2回呼び出されました。

私は次のようにその周りに働いた:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    //... create cell 
    //... create swipe recognizer (in my case attached to cell's contentView) 
    } 

    for (UIGestureRecognizer* g in cell.contentView.gestureRecognizers) 
    g.enabled = YES; 
} 

、その後

-(void) cellWasSwiped: (UIGestureRecognizer*) recognizer { 

    if (recognizer.state != UIGestureRecognizerStateEnded) 
    return; 

    if (recognizer.enabled == NO) 
    return; 

    recognizer.enabled = NO; 

    //... handle the swipe 
    //... update whatever model classes used for keeping track of the cells 
    // Let the table refresh 
    [theTable reloadData]; 
} 
+0

iOS 5の何らかの理由で、私は有効な状態をチェックする必要はありませんでした。私の考えは、iOS 5が有効/無効状態を尊重し、4.3がそうでないことです。有効な状態のチェックを追加すると、すべて正常に動作していました。 –

関連する問題