2013-10-29 12 views
5

का उपयोग कर UITableViewCell अनुक्रमणिकापैथ प्राप्त करें मेरे पास एक दृश्य है जिसमें UITableView शामिल है। इस UITableView की कोशिकाओं को विभिन्न प्रकार के कोशिकाओं के क्रम में इंटरफ़ेस बिल्डर में बनाया गया है। तो प्रत्येक बटन की कार्रवाई निम्नानुसार सेल कक्षाओं में प्रबंधित की जाती है।कस्टम सेल

- कोशिकाओं के विभिन्न प्रकार युक्त मेरे UITableView: -:

प्रकार एक कोशिकाओं ("CellTypeOne.h") के लिए मेरी कक्षा का हेडर फाइल

enter image description here

@interface CellTypeOne : UITableViewCell { } - (IBAction)actionForFirstButton:(id)sender; - (IBAction)actionForSecondButton:(id)sender; - (IBAction)actionForThirdButton:(id)sender; - (IBAction)actionForFourthButton:(id)sender; 

- प्रकार दो कोशिकाओं के लिए मेरी कक्षा का हेडर फाइल ("CellTypeTwo.h"):

@interface CellTypeTwo : UITableViewCell 
{ 
} 

- (IBAction)actionForTheUniqueButton:(id)sender; 

- दृश्य जो मेरे तालिका दृश्य होते हैं ("ViewContainingMyTableView.h") :

@interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    UITableView *myTBV; 
} 

@property (retain, nonatomic) IBOutlet UITableView *myTBV; 

@end 

यहाँ बात मैं चाहता हूं:

जब मैं पहले सेल में पहले बटन पर क्लिक करता हूं, उदाहरण के लिए, मैं "वर्तमान" सेल के इंडेक्सपाथ को दिखाने में सक्षम होना चाहता हूं। 0

  • मैं पहली बार सेल में तीसरे बटन पर क्लिक करें:

    • मैं पहली बार सेल में पहली बटन पर क्लिक करें:

      उदाहरण के लिए, मैं निम्नलिखित उत्पादन के लिए जब चाहते हैं: 0

    • मैं दूसरे सेल में पहला और अनूठा बटन पर क्लिक करें: 1
    • आदि ...
  • +0

    आप बटन पर क्लिक करने पर ही सेल के indexpath की ज़रूरत है? –

    +0

    हां, मुझे उस सेल के इंडेक्सपाथ की आवश्यकता है जिसमें क्लिक किए गए बटन हों। –

    +0

    @ Erzékiel बस कस्टम सेल में एक प्रतिनिधि डालते हैं और जब भी आप कस्टम सेल में बटन पर क्लिक करते हैं तो वहां नियंत्रक को देखने के लिए प्रतिनिधि विधि को ट्रिगर करते हैं, आप पर्यवेक्षक को अपने कस्टम सेल के रूप में प्राप्त कर सकते हैं और आपको चयनित सेल –

    उत्तर

    10

    में के बाद से आप कस्टम सेल का उपयोग कर रहे हैं, मुझे लगता है कि आपको चयन को संभालने की भी आवश्यकता है, क्योंकि आप कस्टम सेल के अंदर बटन को छू रहे हैं, इसलिए सेल स्वयं ही टेबलव्यू प्रतिनिधि विधियों को निकाल नहीं दिया गया है, जैसा कि मैंने आपके कस्टम सेल में कहा है, के लिए एक प्रतिनिधि विधि आपके कस्टम कोशिकाओं में उदाहरण

    अपने CellTypeOne.h में जोड़ने के इस

    //@class CellTypeOne; //if u want t pass cell to controller 
    @protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell 
    - (void)touchedTheCell:(UIButton *)button; 
    //- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `@class CellTypeOne;` at the beginning 
    @end 
    
    @interface CellTypeOne : UITableViewCell 
    { 
    
    } 
    @property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate 
    - (IBAction)actionForFirstButton:(id)sender; 
    - (IBAction)actionForSecondButton:(id)sender; 
    - (IBAction)actionForThirdButton:(id)sender; 
    - (IBAction)actionForFourthButton:(id)sender; 
    
    अपने CellTypeOne.m फ़ाइल

    @synthesize delegate; //synthesize the delegate 
    
    - (IBAction)actionForFirstButton:(UIButton *)sender 
    { 
        //add this condition to all the actions becz u need to get the index path of tapped cell contains the button 
        if([self.delegate respondsToSelector:@selector(touchedTheCell:)]) 
        { 
         [self.delegate touchedTheCell:sender]; 
         //or u can send the whole cell itself 
         //for example for passing the cell itself 
         //[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error in the defining of the delegate add "@class CellTypeOne;" above the defying the delegate 
        } 
    } 
    

    और अपने ViewContainingMyTableView.h

    @interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table delegates 
    { 
        UITableView *myTBV; 
    } 
    
    @property (retain, nonatomic) IBOutlet UITableView *myTBV; 
    
    @end 
    

    में और ViewContainingMyTableView.m फ़ाइल में में

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        //during the creating the custom cell 
        CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"]; 
        if(cell1 == nil) 
        { 
        cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
        } 
        cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important 
    } 
    
    //now implement the delegate method , in this method u can get the indexpath of selected cell 
    
    - (void)touchedTheCell:(UIButton *)button 
    { 
        NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview]; 
        NSLog(@"%@",indexPath.description); 
    
    } 
    /* if u pass the cell itself then the delegate method would be like below 
    - (void)touchedTheCell:(CellTypeOne *)myCell 
    { 
        NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path 
        //now by using the tag or properties, whatever u can access the contents of the cell 
        UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button 
        //... u can access all the contents in cell 
    } 
    */ 
    
    अपने मामले में

    , इस सेल में पहला बटन के लिए है, सेल में एक और बटन के लिए, विभिन्न कार्यों होने प्रत्येक बटन के लिए प्रतिनिधि विधियों को जोड़ने की प्रक्रिया से ऊपर दोहराने

    आशा यू इस मिल :) hapy कोडिंग

    +0

    मैं छुआ में प्रवेश करता हूं, इसलिए यह एक अच्छी बात है। लेकिन '[self.tbvTimeLine indexPathForCell: (UITableViewCell *) बटन। सुपरव्यू] 'शून्य है ... –

    +0

    आपको मेरे लिए काम कर रहे इंडेक्स पथ को प्राप्त करना चाहिए –

    +0

    ब्रेक पॉइंट्स –

    0

    आप उन्हें अलग करने के लिए प्रत्येक सेल पर अपने xib के अंदर टैग का उपयोग कर सकते हैं।

    [self tag] सेल वर्ग के भीतर के बाद

    उपयोग

    +0

    सेल का इंडेक्स पथ मिल सकता है गतिशील रूप से जेनरेट किया गया ... मैं टैग का उपयोग नहीं करना पसंद करता हूं। –

    1

    iOS7 के साथ आप इसे इस तरह प्राप्त कर सकते हैं:

    - (IBAction)actionForTheUniqueButton:(id)sender 
    { 
        YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview]; 
        NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell]; 
    } 
    
    +0

    हम, मैं अपने टेबल व्यू को कैसे देख सकता हूं? क्योंकि यह किसी अन्य दृश्य में है, इसलिए 'self.myTableView' काम नहीं करता है ... –

    +0

    यह काम कर सकता है लेकिन मुझे नहीं पता कि मेरी तालिका का संदर्भ कैसे देखें। मैं इसे आज़माता हूं लेकिन यह काम नहीं करता है: 'ViewContainingMyTableView * v = [[ViewContainingMyTableView alloc] init]; एनएसएलओजी (@ "myTBV:% @", [पी myTBV]); ' –

    +0

    वास्तव में ... पहली बात जो मुझे लगता है कि आप कर सकते हैं (और क्योंकि आपने कोशिकाओं के लिए अपनी कक्षा बनाई है) आपके लिए एक संपत्ति जोड़ना है तालिका का जिक्र करते हुए अनुकूलित कक्ष सेल को प्रदर्शित करना देखें। इस समाधान के लिए इस समाधान का सुझाव दिया गया है: http://stackoverflow.com/questions/1110482/reference-from-uitableviewcell-to-parent-uitableview – zbMax

    0

    ज या मीटर फ़ाइल में tableView IBOutlet पैदा करते हैं।आप उर actionForFirstButton अंदर का उपयोग करके चयनित TableCell की indexpath प्राप्त कर सकते हैं: विधि

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    
    NSLog(@"I click on the first button in the first cell : %d", indexPath.row); 
    

    संपादित: बटन का उपयोग टैग सूचकांक पथ प्राप्त करने के लिए, अपने actionForFirstButton विधि

    if(sender.tag == 0 || 1 || 2 || 3){ 
        { 
         NSLog(@"the cell is : %d", 0); 
        } 
        else if(sender.tag == 4){ 
        { 
         NSLog(@"the cell is : %d", 1); 
        } 
        else{ 
        NSLog(@"the cell is : %d", 2); 
        } 
    
    +0

    I मुझे यकीन नहीं है कि किसी सेल में बटन को स्पर्श करके, उसने सेल का चयन किया ... – zbMax

    +0

    जब मैं एक बटन पर क्लिक करता हूं, तो सेल किसी चयनित स्थिति में नहीं है ... यह मेरी समस्या है! –

    +0

    मैं टैग का उपयोग नहीं करना चाहता ... क्षमा करें –

    0

    आपका आवश्यकता है बस एक बटन की इसी पंक्ति मूल्य,

    तो फिर तुम एक बहुत ही आसान तरीका है ... वापस पाने के लिए

    @interface CellTypeOne : UITableViewCell 
    { 
        NSNumber *rowval; 
    } 
    @property (nonatomic,retain)NSNumber* rowval; 
    

    अपने CellTypeTwo.h में एक ही बात

    @interface CellTypeTwo : UITableViewCell 
    { 
        NSNumber *rowval; 
    } 
    @property (nonatomic,retain)NSNumber* rowval; 
    

    और अब दोनों .m फ़ाइल में आप बटन के लिए एक विधि, परिभाषित किया जाना चाहिए उन विधि में तो साथ सिर्फ इस लाइन पेस्ट

    NSLog(@"Number : %d",[rowval integerValue]); 
    

    और अंदर), tableView cellForRowAtIndexPath: विधि भी

    cell.rowval = [[NSNumber alloc]initWithInt:indexPath.row ]; 
    

    अब हर बार जब आप अलग अलग सेल का एक बटन दबाएँ इस लाइन को जोड़ने एल यह सेल

    आशा इस में मदद मिलेगी की पंक्ति मूल्य के साथ वापस जवाब होगा .. :-)

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