Search code examples
iphoneobjective-cuitableviewuitouch

reloading tableview and touch gestures


I have a tableview which is being reloaded as new content is added, using [tableview reloadData];

Trouble is I have a UILongPressGestureRecognizer on the TableCells in the Table and because the cells / table are being reloaded quite often the LongPress doesnt always have time to work as, I'm guessing it's internal timers are being reset when the cell/table is being reloaded.


Solution

  • Have you tried looking at the state of your UILongPressGestureRecognizers before [tableView reloadData] is called? For example:

    // Returns |YES| if a gesture recognizer began detecting a long tap gesture
    - (BOOL)longTapPossible {
        BOOL possible = NO;
    
        UIGestureRecognizer *gestureRecognizer = nil;
        NSArray *visibleIndexPaths = [tableView indexPathsForVisibleRows];
    
        for (NSIndexPath *indexPath in visibleIndexPaths) {
            // I suppose you have only one UILongPressGestureRecognizer per cell
            gestureRecognizer = [[tableView cellForRowAtIndexPath:indexPath] gestureRecognizers] 
                                    lastObject];
            possible = (gestureRecognizer.state == UIGestureRecognizerStateBegan ||
                        gestureRecognizer.state == UIGestureRecognizerStateChanged);
            if (possible) {
                break;
            }
        }
        return possible;
    }
    
    // ... later, where you reload the tableView:
    
    if ([self longTapPossible] == NO) {
        [tableView reloadData];
    }
    

    Let me know if it works!