2011-11-29 22 views
5

पर डबल टैप मैं UITableViewCell पर एक टैप और डबल टैप प्राप्त करना चाहता हूं। मैंने UITableview के लिए एक कस्टमडेटा स्रोत बनाया है।UITableViewCell

मैं इसे कैसे प्राप्त कर सकता हूं?

उत्तर

14

यह करने के लिए सही तरीका tableView पर अपने UITapGestureRecognizer जोड़ने के लिए है:

-(void)singleTap:(UITapGestureRecognizer*)tap 
{ 
    if (UIGestureRecognizerStateEnded == tap.state) 
    { 
     CGPoint p = [tap locationInView:tap.view]; 
     NSIndexPath* indexPath = [_tableView indexPathForRowAtPoint:p]; 
     UITableViewCell* cell = [_tableView cellForRowAtIndexPath:indexPath]; 
     // Do your stuff 
    } 
} 
+2

हाय, मैं इस विधि की कोशिश की, लेकिन जब भी मैं एक सेल पर दोगुनी TAPP, यह हमेशा एक टैप की विधि में जाने। यह दो बार टैप फ़ंक्शन पर कभी नहीं जाता है। मैंने आपके द्वारा प्रदान किया गया वही कोड लिखा है। कृपया मदद करें। –

+0

इसे काम करने के लिए आपको सेल पर उपयोगकर्ता इंटरैक्शन को अक्षम करने की भी आवश्यकता है। टेबल के इशारे पहचानकर्ताओं को अपना काम करने का मौका मिलने से पहले यह एक टैप खाने वाले सेल को रोक देगा। – rickerbh

+4

यदि आप उपयोगकर्ता इंटरैक्शन को अक्षम करते हैं, तो आप कैसे स्क्रॉल करते हैं? –

6

मैं हासिल किया:

UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 
doubleTap.numberOfTapsRequired = 2; 
doubleTap.numberOfTouchesRequired = 1; 
[self.tableView addGestureRecognizer:doubleTap]; 

UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
[singleTap requireGestureRecognizerToFail:doubleTap]; 
[self.tableView addGestureRecognizer:singleTap]; 
फिर अपने कॉलबैक तरीकों में

आप कुछ इस तरह के साथ सेल लगता है यह आपके इशारा पहचानकर्ता को निम्नलिखित जोड़कर, हालांकि मुझे लगता है कि सिंगल टैपिंग में थोड़ी देर हो चुकी है।

[tapRecognizer setDelaysTouchesBegan:YES]; 
[tapRecognizer setCancelsTouchesInView:YES]; 
0

आप [डबलटैप रिलीज] के बारे में भूल गए;

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 

doubleTap.numberOfTapsRequired = 2; 

doubleTap.numberOfTouchesRequired = 1; 

[cell addGestureRecognizer:doubleTap]; 

[doubleTap release]; 
0

आपको सिंगल और डबल टैप्स के बीच अलग करने के लिए इस पंक्ति की आवश्यकता है। [सिंगलटैप की आवश्यकता है GestureRecognizerToFail: DoubleTap]; उदाहरण कोड यहां दिया गया है।

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // After cell initialized code 
    //... 
    //... 

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; 
    doubleTap.numberOfTapsRequired = 2; 

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap:)]; 
    singleTap.numberOfTapsRequired = 1; 
    [singleTap requireGestureRecognizerToFail:doDoubleTap]; 

    [cell addGestureRecognizer:singleTap]; 
    [cell addGestureRecognizer:doubleTap]; 
    return cell; 
} 

-(void)doubleTap:(UITapGestureRecognizer*)tap 
{ 
    NSLog(@"Double Taps"); 

    CGPoint point = [tap locationInView:self.tableView]; 
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint: point]; 
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    // do your stuff. 
    // ... 
} 

-(void)singleTap:(UITapGestureRecognizer*)tap 
{ 
    NSLog(@"Single Tap"); 
    CGPoint point = [tap locationInView:self.tableView]; 
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:point]; 
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    // Do you stuff with index path or cell here. 
    // ... 

} 
+0

यह गलत है! दृश्य में तालिका दृश्य में 2 पहचानकर्ता जोड़नाDidLoad पर्याप्त है। आप 2 एक्स tableview.count पहचानकर्ता बना रहे हैं! यदि आपके पास कई कक्ष हैं, तो आप कुछ भी नहीं के लिए स्मृति और यूआई प्रतिक्रिया लेते हैं ... – altagir