2012-06-17 7 views
15

पर क्लिक किया गया है मैंने कस्टम UITableViewCell के साथ UITableView बनाया है। मेरे सेल में बाईं ओर UIImageView और दाईं ओर UITextView शामिल है।मैं कैसे पहचान सकता हूं कि UITableViewCell का कौन सा हिस्सा

UITableViewController के अंदर, मैंने टेबलव्यू cellForRowAtIndexPath में छवि और टेक्स्ट दोनों सेट किए हैं।

सब कुछ ठीक दिखाता है लेकिन अब मुझे didSelectRowAtIndex को लागू करने की आवश्यकता है और मुझे सेल की क्लिक की गई UIImageView या UITextView पर अंतर करने की आवश्यकता है।

मान लें कि, छवि क्लिक हटाने की कार्रवाई और शेष सेल संपादन कार्रवाई का प्रतिनिधित्व कर रहा है।

उत्तर

48

प्रत्येक व्यक्तिगत सेल में इशारा पहचानकर्ताओं को जोड़ने के बजाय, आप तालिका दृश्य में एक जोड़ सकते हैं और यह निर्धारित कर सकते हैं कि उपयोगकर्ताओं के बिंदु से कौन सा सेल चुना गया था, और फिर यह निर्धारित करें कि उपयोगकर्ता ने छवि या सेल को स्पर्श किया है या नहीं।

पहले सुनिश्चित करें कि आपका नियंत्रक UIGestureRecognizerDelegate प्रोटोकॉल को गोद ले।

@interface MyTableViewController() <UIGestureRecognizerDelegate> 
@end 

फिर UIGestureRecognizerUITableView जब दृश्य लोड करने के लिए जोड़ें।

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
    singleTap.delegate = self; 
    singleTap.numberOfTapsRequired = 1; 
    singleTap.numberOfTouchesRequired = 1; 
    [self.tableView addGestureRecognizer:singleTap]; 
} 

यह प्रतिनिधि विधि निर्धारित करती है कि handleTap: विधि निष्पादित की जानी चाहिए। यदि उपयोगकर्ताओं को स्पर्श करने से indexPath मिल सकता है, तो यह YES देता है अन्यथा यह NO देता है।

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    UITableView *tableView = (UITableView *)gestureRecognizer.view; 
    CGPoint p = [gestureRecognizer locationInView:gestureRecognizer.view]; 
    if ([tableView indexPathForRowAtPoint:p]) { 
     return YES; 
    } 
    return NO; 
} 

एक बार जब हम उपयोगकर्ता एक सेल में क्लिक किया है, तो यह निर्धारित किया है, handleTap: विधि कहा जाता है, जो तब का फैसला करता है उपयोगकर्ता छवि, या सेल के किसी अन्य भाग को छुआ है।

- (void)handleTap:(UITapGestureRecognizer *)tap 
{ 
    if (UIGestureRecognizerStateEnded == tap.state) { 
     UITableView *tableView = (UITableView *)tap.view; 
     CGPoint p = [tap locationInView:tap.view]; 
     NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p]; 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     CGPoint pointInCell = [tap locationInView:cell]; 
     if (CGRectContainsPoint(cell.imageView.frame, pointInCell)) { 
      // user tapped image 
     } else { 
      // user tapped cell 
     } 
    } 
} 
+0

उन है कि पता नहीं है के लिए क्या देख रहे हैं। 'ViewDidLoad' में सभी' UITapGestureRecognizer 'सेटअप को Xcode UI लेआउट (उर्फ आईबी) में भी किया जा सकता है। आप 'हैंडलटैप:' से 'IBAction' के रिटर्न प्रकार को बदलना चाहेंगे। – ThomasW

1

एक बहुत ही सार और सामान्य जवाब आप indexPath.row

//When creating the label and image add a recognizer to them 
label.tag = indexPath.row; 
imageView.tag = indexPath.row; 

तब हो तो

की तरह, प्रत्येक छवि और लेबल पर UITapGestureRecognizer जोड़ने के लिए अपने टैग सेट जोड़े जाने वाले प्रत्येक UIImage और UILabel के लिए निम्नलिखित करना है
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                       action:@selector(handleTap:)]; 
    [label addGestureRecognizer:recognizer]; 
    [imageView addGestureRecognizer:recognizer]; 
} 

- (void) handleTap:(UITapGestureRecognizer*)recognizer 
{ 
    UIView *view = recognizer.view; 
    int row = view.tag; 
    if ([view isKindOfClass:[UILabel class]]) { 
     //Row is row 
     //and the label is pressed 
    } 

    if ([view isKindOfClass:[UIImageView class]]) { 
     //Row is row 
     //and the imageview is pressed 
    } 
} 
5

आप UITableViewCell उपखंड कर सकते हैं और touchesEnded ओवरराइड कर सकते हैं।

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    [super touchesEnded:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 

    UIView *hitView = [self hitTest:location withEvent:event]; 

    if (hitView == myImageView) ...; 
    if (hitView == myTextView) ...; 
} 

आप (वे शायद अपने सेल के गुणों होना चाहिए) आपके UIImageView और UITextView के लिए कुछ संदर्भ रखने की जरूरत है।

आप touchesEnded के बजाय touchesBegan ओवरराइड कर सकते हैं, इस पर निर्भर करता है कि आप किस कार्यक्षमता को प्राप्त करना चाहते हैं।

0

आप दो विकल्प होता है लागू करने के लिए है: - 1 - अपने "UITableViewCell" में UITapGestureRecognizer जोड़ सकते हैं और यह छवि दृश्य को इंगित और "indexpath" पारित पैरामीटर के रूप में चयनकर्ता और यह

tableviewcell के लिए प्रतिनिधि पारित बनाने के लिए
UILabel *label = =[UILabel alloc]init]; 
label.userInteractionEnabled = YES; 
UITapGestureRecognizer *tapGesture = 
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]  autorelease]; 
[label addGestureRecognizer:tapGesture]; 


-(void)labelTap{ 
[delegate [email protected](labeltapped:)withobject:indexpath]; 
} 

2- दूसरा तरीका है DidSelectRowAtIndexPath प्रकार (imageview या लेबल] के प्रेषक को जांचना; लेकिन मुझे पहला तरीका पसंद है

1

छवि को UIButton बनाएं। जब बटन की कार्रवाई ट्रिगर होती है, तो आप जानते हैं कि उपयोगकर्ता ने छवि को टैप किया है (सेल का चयन नहीं किया जाएगा)। यदि सेल चुना जाता है, तो आप जानते हैं कि उपयोगकर्ता पंक्ति पर कहीं और टैप किया गया है (टेक्स्ट व्यू या लेबल सहित)।

इसके अलावा, बटन की टैग प्रॉपर्टी को सेट करें, उदा। सेल की पंक्ति अनुक्रमणिका ताकि आप जान सकें कि कौन सी पंक्ति की छवि टैप की गई थी।

+0

लेकिन के रूप में यह है, यह स्वाइप संभाल नहीं करता है, अगर है कि आप के लिए ... –

संबंधित मुद्दे

 संबंधित मुद्दे