1

I'm developing an iOS 5 app, in my case if user tap a row from UITableView first time then it will perform some action and if it is tapped second time then it will perform different action.

Now, the problem is that how to know if s particular row is tapped second time?

Thanks in advance!!

Blios
  • 719
  • 1
  • 16
  • 30
  • 5
    Hmm... storing the state in your cell or controller is too obvious, isn't it? – Eiko Oct 18 '12 at 10:46
  • [duplicate question](http://stackoverflow.com/questions/1031254/how-can-i-detect-a-double-tap-on-a-certain-cell-in-uitableview) – Cfr Oct 18 '12 at 10:53
  • @Cfr hey man, it is not duplicate of that question. I'm not interested in double click instead it is about if user tap a row and after some time(in between he may select another rows) if he tap again on that row then it will call a different action. – Blios Oct 18 '12 at 11:02
  • oh, sorry. anyway, you can port that solution to yours – Cfr Oct 18 '12 at 11:05
  • Thanks @Eiko, i'll try your suggestion and hope it should work for me. – Blios Oct 18 '12 at 11:06
  • You can try with a global integer variable named int tapCounter, use it in didSelecrRowAtIndexPath delegate method. – Rahul Oct 18 '12 at 11:17
  • @Blios Just add Gestures to your TableView using below solution. – Ajay Sharma Oct 18 '12 at 11:22

2 Answers2

5

Then why don't you use UIGestures with the TableView.This will be the easiest solution which worked for me like charm.

Here is the code which will help you to implement it in your code :

    // Put this code in your ViewDidLoad:
    UITapGestureRecognizer *doublegesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTableViewCellDoubleTapping:)];
    doublegesture.numberOfTapsRequired = 2;
    [roomTableView addGestureRecognizer:doublegesture];
    [doublegesture release];

//Method to handle the event on double tapping

-(void)didTableViewCellDoubleTapping:(UITapGestureRecognizer *)gestureRecognizer 
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:roomTableView];
        NSIndexPath *swipedIndexPath = [roomTableView indexPathForRowAtPoint:swipeLocation];
        NSLog(@"%d",swipedIndexPath.row);
                
        // ... Here you can add your logic
    }
}

In the similar way, if you add another UIGesture for Single tapping

 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTableViewSingleCellTapping:)];
    gesture.numberOfTapsRequired = 1;
    [roomTableView addGestureRecognizer:gesture];
    [gesture release];
    
    [gesture requireGestureRecognizerToFail:doublegesture];

-(void)didTableViewSingleCellTapping:(UITapGestureRecognizer *)gestureRecognizer 
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:roomTableView];
        NSIndexPath *swipedIndexPath = [roomTableView indexPathForRowAtPoint:swipeLocation];
        
        NSLog(@"%d",swipedIndexPath.row);
    }
}
Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
1

Here is my solution(as Eiko suggest):

Maintain a mutable array and store row index whenever a row is first time selected in array and call first action. When row is selected second time or another row is get selected check if index of that row is present in array, if it is than call second action else add index of row to array and call first action.

Blios
  • 719
  • 1
  • 16
  • 30