6

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.

daihovey
  • 3,485
  • 13
  • 66
  • 110
  • What is the frequency of updates? – Deepak Danduprolu Jun 15 '11 at 07:19
  • What kind of changes are happening on the cells? Does the whole table change, or just one cell at the time? – EmilioPelaez Jun 15 '11 at 07:27
  • The tables datasource being added to and when it is the tableviewcontroller is calling [tableview reloadData] which reloads the table / adds a cell to the bottom of the table. – daihovey Jun 15 '11 at 07:33
  • What problems do you have, and what *should* happen? – Eiko Jun 25 '11 at 17:11
  • Is there no way of detecting if the user has placed a finger on the device? That way I can stop reloadData being called until the finger is lifted... – daihovey Jun 28 '11 at 00:55
  • Referring to your latest question daidai; see octy's answer - check for the `UIGestureRecognizer.state` and you are good to go. – Till Jul 02 '11 at 01:17

3 Answers3

4

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!

octy
  • 6,525
  • 1
  • 28
  • 38
3

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.
Anomie
  • 92,546
  • 13
  • 126
  • 145
  • I have tried this technique in the past and found that the reloadData method was much better for my system. Is there no way of detecting if the user have place a finger on the device? – daihovey Jun 28 '11 at 00:54
0

Set a BOOL like aCellIsSelected to YES when you touch the cell

and just reload tableview if aCellIsSelected is NO

FoJjen
  • 774
  • 1
  • 6
  • 14
  • The trouble with this is that the UILongPressGestureRecognizer doesnt get fired until the a 'long' time has passed. And I cant listen for the didSelectRowAtIndexPath as this is a tap and is not a press, so it doesnt get called either. – daihovey Jun 16 '11 at 02:16
  • You can subclass UITableViewCell and in set the value in setHighlighted – FoJjen Jun 16 '11 at 06:46