2011-01-05 15 views
43

मैं बहुत तरह cellForRowAtIndexPath: विधि में एक एक UITableViewCell करने के लिए UISwipeGestureRecognizer संलग्न कर रहा हूँ:UIGestureRecognizer और UITableViewCell मुद्दा

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; 
     gesture.direction = UISwipeGestureRecognizerDirectionRight; 
     [cell.contentView addGestureRecognizer:gesture]; 
     [gesture release]; 
    } 
    return cell; 
} 

हालांकि, didSwipe विधि हमेशा दो बार सफल कड़ी चोट पर कहा जाता हो रही है। मैं शुरू में सोचा था इस वजह से इशारा प्रारंभ और समाप्त होता था, लेकिन अगर मैं gestureRecognizer ही लॉग आउट, वे "समाप्त" राज्य में दोनों कर रहे हैं:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { 

    NSLog(@"did swipe called %@", gestureRecognizer); 
} 

कंसोल:

2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right> 
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right> 

मैं वास्तव में वास्तव में पता नहीं क्यों। मैंने स्पष्ट रूप से समाप्त राज्य की जांच करने की कोशिश की, लेकिन यह कोई मदद नहीं है क्योंकि वे दोनों "समाप्त" के रूप में आते हैं ... कोई विचार?

उत्तर

109

सीधे सेल पर इशारा पहचानकर्ता जोड़ने के बजाय, आप इसे viewDidLoad में तालिकादृश्य में जोड़ सकते हैं।

didSwipe में इस प्रकार -Method आप प्रभावित IndexPath और सेल का निर्धारण कर सकते हैं:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { 

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; 
     NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
     UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; 
     // ... 
    } 
} 
+0

धन्यवाद! इसने इसे दो बार फायरिंग से रोक दिया! :) – mootymoots

+0

धन्यवाद, इससे मदद मिली, लेकिन मैं कुछ अजीब देख रहा हूं - स्वाइप करने के बाद मुझे पंक्ति का चयन करना चाहते हैं तो मुझे दो बार दबाएं। पहली बार कुछ भी नहीं करता है, और दूसरी कॉल करता है चयन करें ... किसी ने इसे देखा है? –

+1

@OdedBenDov मेरा मानना ​​है कि यह इशारा पहचानकर्ता के कारण होता है, मेरे पास तालिका दृश्य सेल चयन के लिए अजीब व्यवहार था जब मैंने टैप इशारा पहचानकर्ता जोड़ा और टेक्स्ट फ़ील्ड (टेबल व्यू सेल के उप दृश्य) की अंत संपादन विधि में निकालना भूल गया, शायद यह आपकी समस्या को हल करने के लिए एक सुराग के रूप में कार्य कर सकता है, अधिक जानकारी [यहां] (http://stackoverflow.com/questions/9939509/strange-behavior-did-select-row-touch-not-responding-for-uitableviewcell) –

0

यह एप्लिकेशन प्रतिनिधि के साथ काम करेंगे

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// code 

} 
+2

ऐप प्रतिनिधि? एचएम? – quantumpotato

+0

टेबलव्यू प्रतिनिधि –

0

मैं इस एक ही समस्या थी और टिक टिक द्वारा इसे हल तालिका दृश्य विशेषताओं में "स्क्रॉलिंग सक्षम"।

मेरी तालिका दृश्य को स्क्रॉलिंग की आवश्यकता नहीं है, इसलिए यह ऐप को किसी भी अन्य तरीके से प्रभावित नहीं करता है, सिवाय इसके कि मुझे स्वाइप इशारा के बाद पहला अनुत्तरदायी टैप नहीं मिलता है।