2013-09-05 8 views
27

मैं निम्नलिखित के रूप में UITableViewCell चयनित खोजने की कोशिश कर रहा हूँ:आईओएस, UITableView की चयनित पंक्ति कैसे खोजें?

- (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]; 

    } 
    if ([indexPath row] == [tableView cellForRowAtIndexPath:indexPath.row]) // i want to place value of selected row to populate the buttons 

     //([indexPath row] == 0) 

     //(indexPath.row == ![self cellIsSelected:indexPath]) 

    { 
     UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image 

     [AddComment addTarget:self 
         action:@selector(TestBtns:) 
      forControlEvents:UIControlEventTouchDown]; 

     // [AddComment setTitle:@"1" forState:UIControlStateNormal]; 

     AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0); 

     [cell.contentView addSubview:AddComment]; 

     UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"]; 

     [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal]; 

     [cell.contentView addSubview:AddComment]; 


    } 

इस लक्ष्य को हासिल करने के लिए कैसे, आप मेरा मार्गदर्शन कृपया मैं कहाँ गलती कर रहा हूँ कर सकते हैं।

+0

कहाँ didselectrowatindexpath ** विधि है ** ??? –

+0

आप UITableview के चयनित सेल को ठीक करना चाहते थे ..? ** didselectrowatindexpath ** या बटन TouchUiInside विधि पर? –

+0

कृपया लाइन को भी जांचें "अगर ([इंडेक्सपाथ पंक्ति] == [तालिका दृश्य कक्षफॉररोएट इंडेक्सपैथ: इंडेक्सपाथ])"। यहां आप एक USIableViewCell के साथ एक NSInteger की तुलना कर रहे हैं! – AlexVogel

उत्तर

15

उपयोग इस प्रतिनिधि UITableViewDataSource प्रतिनिधि में UITableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%d", indexPath.row); // you can see selected row number in your console; 
} 
6

की विधि एक विधि है - (शून्य) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath

यह NSIndexPath ऑब्जेक्ट देता है जो चयनित अनुभाग और चयनित पंक्ति दोनों को प्राप्त करता है।

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSLog(@"Selected section>> %d",indexPath.section); 
    NSLog(@"Selected row of section >> %d",indexPath.row); 
} 

यह अन्यथा इस विधि

1

यहाँ निर्मित विधि में मदद मिलेगी कि कौन-सेल का चयन किया गया है कहा जाता है नहीं की जाएगी उपयोग करने से पहले tableview के डेटा स्रोत सेट करने के लिए सुनिश्चित करें।

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // you can use "indexPath" to know what cell has been selected as the following 
    NSLog(@"Selected row is %@", indexPath); 
} 

और जिस तरह पद्धति पहले से ही दिया जाता है जब आप TableViewController बनाते हैं, तो आप शायद अभी uncomment करना होगा द्वारा।

आप पाते हैं यह उपयोगी

102
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 
+5

क्यों आप इसे सही उत्तर के रूप में स्वीकार नहीं करते? @pratap मनीष भडोरिया –

+0

मेथिंक @ प्रतापमनीशोधिया इस जवाब को देखने के लिए भूल गए। –

+0

@ सूरजके थॉमस cuz यह काम नहीं करता है! –

0

मुझे लगता है कि तुम क्या करने की कोशिश कर रहे हैं, लेकिन पता करने के लिए जो सेल क्लिक किया जाता है पर क्लिक किया तालिका दृश्य सेल के सूचकांक प्राप्त करने के लिए नहीं है आशा है कि।

बताए के समय में यह जानता हूँ प्रत्येक कोशिका तो बनाया सेल के टैग के रूप में indexpath.row मूल्य निर्दिष्ट अभिभावक के रूप में तालिका दृश्य लेने क्लिक किया पर और उस के साथ अपने subview (सेल) प्राप्त करने की कोशिश करने के लिए [टैग के साथ tableview दृश्य: indexpath.row]

को टैग

2
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int a = indexPath.row; 
    NSLog(@"index: %i",a); 
} 
संबंधित मुद्दे