Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 205
daihovey Avatar asked Jun 15 '11 07:06

daihovey


2 Answers

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!

like image 193
octy Avatar answered Nov 15 '22 03:11

octy


Don't use reloadData if you want existing cells to remain. Instead, when you get new data, use the methods for Inserting and Deleting Cells to inform the table view exactly which cells have changed. The general procedure is:

  1. You get new data.
  2. Call beginUpdates
  3. Call deleteRowsAtIndexPaths:withRowAnimation: to remove cells for any old items that have been deleted in the new data.
  4. Call insertRowsAtIndexPaths:withRowAnimation: to add new cells for any items that have been added in the new data.
  5. If you need to selectively replace a particular cell for some reason (and can't just update the existing cell's subviews with the new data), use reloadRowsAtIndexPaths:withRowAnimation:.
  6. Call commitUpdates. At this point, your UITableViewDataSource methods must reflect the new data (e.g. tableView:numberOfRowsInSection: should reflect the changed count, and tableView:cellForRowAtIndexPath: should use the new items).
  7. The table view will now call your data source methods as needed to update the display. Existing rows will not be changed.
like image 28
Anomie Avatar answered Nov 15 '22 04:11

Anomie