Reputation: 1079
I have added single and double tap gesture recognisers to UITableViewCells. But after scrolling through the table a few times, there becomes an increasingly longer pause between the end of my swipe gesture to scroll the table, and the start of the scrolling animation.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"tableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[cell addGestureRecognizer:singleTap];
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[cell addGestureRecognizer:doubleTap];
if (tableView == self.searchDisplayController.searchResultsTableView) {
// search results
}
else {
// normal table
}
return cell;
}
SingleTap: and doubleTap: methods
- (void)singleTap:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableViewCell *cell = (UITableViewCell *)tap.view;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath* indexPath = [tableView indexPathForCell:cell];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// do single tap
}
}
- (void)doubleTap:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableViewCell *cell = (UITableViewCell *)tap.view;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath* indexPath = [tableView indexPathForCell:cell];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// do double tap
}
}
Since the initial scrolling is smooth I tried adding the gesture recognisers into the if (cell == nil)
condition, but then they where never added to the cells.
I also initially had the gestures added to the tableView rather than the individual cells, but this caused issues with the searchDisplayController i.e. tap on the cancel button not recognised.
I'd appriciate any thoughts, thanks.
Upvotes: 1
Views: 2165
Reputation: 35394
The cellForRowAtIndexPath-method gets called multiple times for the same NSIndexPath so you add too many gesture recognizers to the cells. Thus the performance will suffer.
My first suggestion would be to add only one gesture recognizer to the table view. (I wrote this answer for a simililar question: https://stackoverflow.com/a/4604667/550177 )
But as you said, it causes issues with the searchDisplayController. Maybe you can avoid them with a smart implementaion of the UIGestureRecognizerDelegate (return NO in -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
when the tap occurred not within a cell).
My second suggestion: Add the gesture recognizers only once:
if ([cell.gestureRecognizers count] == 0) {
// add recognizer for single tap + double tap
}
Upvotes: 6