2

我在GestureRecognizerUITableViewCell中遇到了問題,我有左右滑動手勢,點按2秒鐘並捏住表格單元格中的手勢,滑動右下一章,向左滑動上一章,捏重定向到另一個視圖控制器,並保持2秒的彈出窗口顯示(子視圖).all在一個稱爲table的tableview中實現。我的問題是每一件事情都可以正常工作,但是在桌面視圖中使用Gesture很長一段時間之後,它可能會陷入尷尬的局面。但當我去到另一個頁面,並回來它開始正常工作。並且在一段時間之後也會發現。如何解決這個我的代碼是?uitableview單元格中的手勢錯誤

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

    static NSString *CellIdentifier = @"Cell"; 

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; 
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; // or whatever 
    [table addGestureRecognizer:swipeGesture]; 
    [swipeGesture release]; 

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleLongPress:)]; 
    lpgr.minimumPressDuration = 1.0; //seconds 
    lpgr.delegate = self; 
    [table addGestureRecognizer:lpgr]; 
    [lpgr release]; 

    UISwipeGestureRecognizer *swipeGestureleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureleft:)]; 
    swipeGestureleft.direction = UISwipeGestureRecognizerDirectionLeft; // or whatever 
    [table addGestureRecognizer:swipeGestureleft]; 
    [swipeGestureleft release]; 

    UIPinchGestureRecognizer *longPressRecognizer = 
    [[UIPinchGestureRecognizer alloc] 
    initWithTarget:self 
    action:@selector(longPressDetected:)]; 
    [self.view addGestureRecognizer:longPressRecognizer]; 
    [longPressRecognizer release]; 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint p = [gestureRecognizer locationInView:table]; 

    NSIndexPath *indexPath = [table indexPathForRowAtPoint:p]; 
    if (indexPath == nil) 
     NSLog(@"long press on table view but not on a row"); 
    else 

     // localStringValueverseno= [tableView cellForRowAtIndexPath:indexPath]; 
     localStringValueverseno = [(UILabel *)[[table cellForRowAtIndexPath:indexPath] 
               viewWithTag:1] text]; 
    localStringValue = [table cellForRowAtIndexPath:indexPath].textLabel.text; 
    _lblmainnotegns.text = localStringValueverseno; 
    // localStringValueverseno= [tableView cellForRowAtIndexPath:indexPath].cell.table.chapterAndVerse.text; 
    delegate.selectedIndex=[NSString stringWithFormat:@"%d",indexPath.row+1]; 
    [userInfo setObject:[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] forKey:@"text"]; 
    [userInfo setObject:delegate.selectedBook forKey:@"book"]; 
    [userInfo setObject:delegate.selectedChapter forKey:@"chapter"]; 
    [userInfo setObject:[NSString stringWithFormat:@"%d",indexPath.row + 1] forKey:@"verse"]; 

    UIView *noteView = (UIView *)[self.view viewWithTag:101]; 
    //ViewWithTag Number should be same as used while allocating 
    [noteView removeFromSuperview]; 
    //For removing View 
    UIView *acntView = (UIView *)[self.view viewWithTag:100]; 
    //ViewWithTag Number should be same as used while allocating 
    [acntView removeFromSuperview]; 
    // now you can use cell.textLabel.text 
    UIView *noteViewsync = (UIView *)[self.view viewWithTag:105]; 
    //ViewWithTag Number should be same as used while allocating 
    [noteViewsync removeFromSuperview]; 


    if ([notes objectForKey:[NSString stringWithFormat:@"%@ %@:%@",delegate.selectedBook,delegate.selectedChapter,delegate.selectedIndex]] == nil) { 
     [notetickimage setHidden:YES]; 
     [self.view addSubview:MainPopupView]; 
    } 
    if (gestureRecognizer.state==UIGestureRecognizerStateBegan) { 
     self.table.scrollEnabled = NO; 
     [table removeGestureRecognizer:gestureRecognizer]; 
    } 
    else if(gestureRecognizer.state==UIGestureRecognizerStateEnded) { 
     self.table.scrollEnabled = YES; 
     [table removeGestureRecognizer:gestureRecognizer]; 
    } 



     NSLog(@"long press on table view at row %d", indexPath.row); 
} 
-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer { 


    if(![delegate.selectedChapter isEqualToString:[NSString stringWithFormat:@"%d",[DbHandler mNumberOfChaptersInBook:delegate.selectedBook]]]) { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.4]; 
     [UIView setAnimationDelay:0.0]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDidStopSelector:@selector(animCompleteHandler:finished:context:)]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; 
     [UIView commitAnimations]; 

     // if the currentChapter is the last then do nothing 
     delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1]; 
     [delegate reloadVerses]; 
     [self resetReadViewToVerse:1]; 
     [table removeGestureRecognizer:recognizer]; 
    } 
    if (recognizer.state==UIGestureRecognizerStateBegan) { 
     self.table.scrollEnabled = NO; 
     [table removeGestureRecognizer:recognizer]; 
    } 
    else if(recognizer.state==UIGestureRecognizerStateEnded) { 
     self.table.scrollEnabled = YES; 
     [table removeGestureRecognizer:recognizer]; 
    } 


    return; 



} 
-(void) handleSwipeGestureleft:(UISwipeGestureRecognizer*)recognizer { 

    if(![delegate.selectedChapter isEqualToString:@"1"]) 
    { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.4]; 
     [UIView setAnimationDelay:0.0]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDidStopSelector:@selector(animCompleteHandler:finished:context:)]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 
     [UIView commitAnimations]; 
     delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1]; 
     [delegate reloadVerses]; 
     [self resetReadViewToVerse:1]; 
     [table removeGestureRecognizer:recognizer]; 
    } 
    if (recognizer.state==UIGestureRecognizerStateBegan) { 
     self.table.scrollEnabled = NO; 
     [table removeGestureRecognizer:recognizer]; 
    } 
    else if(recognizer.state==UIGestureRecognizerStateEnded) { 
     self.table.scrollEnabled = YES; 
     [table removeGestureRecognizer:recognizer]; 
    } 

    return; 


} 
-(void) longPressDetected:(UIPinchGestureRecognizer*)recognizer { 


    self.table.scrollEnabled = NO; 
    SearchViewController *aSecondViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil]; 
    aSecondViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:aSecondViewController animated:YES]; 

    /*[self.navigationController pushViewController:aSecondViewController animated:YES];*/ 

    [aSecondViewController release]; 
    [UIView commitAnimations]; 
    [table removeGestureRecognizer:recognizer]; 

    UIView *noteView = (UIView *)[self.view viewWithTag:101]; 
    //ViewWithTag Number should be same as used while allocating 
    [noteView removeFromSuperview]; 
    UIView *setngView = (UIView *)[self.view viewWithTag:102]; 
    //ViewWithTag Number should be same as used while allocating 
    [setngView removeFromSuperview]; 
    UIView *acntView = (UIView *)[self.view viewWithTag:100]; 
    //ViewWithTag Number should be same as used while allocating 
    [acntView removeFromSuperview]; 
    UIView *popView = (UIView *)[self.view viewWithTag:106]; 
    //ViewWithTag Number should be same as used while allocating 
    [popView removeFromSuperview]; 

} 

而且當我們向左或向右滑動時動作也很慢。 請幫幫我。

回答

1

每次調用例程tableView:cellForRowAtIndexPath:時,都會添加一個識別器,並在處理手勢後刪除它。因此,在所有識別器(它們的數量等於細胞數目)被刪除之後,手勢將不會被識別。

也許你不應該在處理例程中刪除識別器?

1

您的手勢識別器適用於整個表格,但您每次創建單元格時都會添加它們!

每次創建單元格時,都不要將手勢識別器添加到整個表格中,而是嘗試將其添加到其他位置。 或者如果你的手勢只是單元格,請嘗試使用:

[cell addGestureRecognizer:recognizer];

相關問題