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.
Have you tried looking at the state of your UILongPressGestureRecognizer
s 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!
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:
beginUpdates
deleteRowsAtIndexPaths:withRowAnimation:
to remove cells for any old items that have been deleted in the new data.insertRowsAtIndexPaths:withRowAnimation:
to add new cells for any items that have been added in the new data.reloadRowsAtIndexPaths:withRowAnimation:
.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).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With