Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable multiple taps on uitableviewcell

I have a uitableview that implements a popover (PopoverView) when a cell is tapped and then the popover will dismiss on any other tap on the screen. The issue is that if a user would to double tap or tap repeatedly on the cell, it will cause multiple instances of popoverviews to display and then the application will crash.. I am looking for a way to either disable double tapping on the cell and/or the UITableView in general OR is there a way to delay touches on a UITableViewCell any ideas?

I already tried this but it does not work in my case. Another approach would to be to check if PopoverView is already present, if so then don't allow another one to instantiate. I tried this and this and both do not work in my case.

Here is my code where I call the popover view on didSelectRowAtIndexpath:

- (void)tableView:(UITableView *)TableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
sti = [[SelectedTeamsInfo alloc] init];
MyLeagueStandings *info = [fetchedResultsController objectAtIndexPath:indexPath];
[sti getAllScheduleForTeam:info.urlforteam];
NSString *title = info.teamname;

// If title length is greater then 32 truncate it to fit.
if (title.length > 32) {
    title = [info.teamname substringToIndex:29];
    title = [title stringByAppendingString:@"..."];
}


[PopoverView showPopoverAtPoint:cell.center inView:self.view withTitle:title withContentView:sti.view delegate:self];
}

SOLUTION:

In interface class:

 BOOL PopoverYN;

In Implementation class:

- (void)tableView:(UITableView *)TableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // If the popover is not available then display it else do nothing since one is already displayed.
        if (PopoverYN == NO) {
            PopoverYN = YES;
            UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
            sti = [[SelectedTeamsInfo alloc] init];
            MyLeagueStandings *info = [fetchedResultsController objectAtIndexPath:indexPath];
            [sti getAllScheduleForTeam:info.urlforteam];
            NSString *title = info.teamname;

            // If title length is greater then 32 truncate it to fit.
            if (title.length > 32) {
                title = [info.teamname substringToIndex:29];
                title = [title stringByAppendingString:@"..."];
            }
            [PopoverView showPopoverAtPoint:cell.center inView:self.view withTitle:title withContentView:sti.view delegate:self];
        }

}

#pragma mark - popover methods.
- (void)popoverViewDidDismiss:(PopoverView *)popoverView;
{
    PopoverYN = NO;
}
like image 301
sudo Avatar asked Oct 04 '22 13:10

sudo


1 Answers

I have one more solution. Hope this will help someone. If you want to detect the second tap and consume it then this is it, this worked for me. I was loading web view on single tap, if there are two consecutive taps, the bug was getting NSURLErrorCancelled event and was causing white flash screen on webview. I could handle it at web view level but I though I should kill the problem at root.

NSTimer *avoidDoubleTapTimer;

- (void) onTimer {  
    NSLog(@"Timer out");
    avoidDoubleTapTimer = nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(avoidDoubleTapTimer == nil) {
        avoidDoubleTapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
    } else {
        NSLog(@"Double Tap Detected");
        return;
    }
// do you stuff on single tap
}
like image 80
mask Avatar answered Oct 07 '22 17:10

mask