2011-07-05 10 views
13

मैं लगभग UITableViewCell को UITextField के साथ कार्यान्वित कर रहा हूं। बल्कि तो जा रहा CGRectMake के माध्यम से और UITableViewCell.contentView मैं इसे सरल तरीके से लागू किया है:UITextViewCell UITextField के साथ UITableView पंक्ति का चयन करने की क्षमता खो रहा है?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
    amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 
    [amountField setDelegate:self]; 

    [[cell textLabel] setText:@"Amount"]; 
    [cell addSubview:amountField]; 
    return cell; 
} 

और फिर मैं भी didSelectRow पद्धति लागू, पाठ फ़ील्ड इस्तीफा दे दिया अन्य क्षेत्रों इनपुट दृश्यों दिखा अनुमति देने के लिए।

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    [amountField resignFirstResponder]; 
    ... 
} 

यह सुचारू रूप से काम करता है, केवल एक चीज है, तालिका में अन्य पंक्तियों देखते हैं तब होता है जब उन अन्य लोगों का चयन किया जाता पूरे सेल चुना गया है और ब्लू बदल जाता है, जबकि मेरी UITextField के साथ एक नहीं है, मैं क्षेत्र मतलब चुना गया है और मैं पाठ दर्ज कर सकता हूं लेकिन सेल का चयन नहीं किया गया है। मैं इसे परीक्षण किया है और पता लगा समस्या कतार में है है:

[cell addSubview:amountField]; 

ऐसा लगता है कि इस चयन सेल व्यवहार टूट जाता है, और यहां तक ​​कि [cell contentView] में जोड़ने से इसे ठीक नहीं है। क्या मैं कुछ भुल गया?

उत्तर

39

पाठ क्षेत्र userInteractionEnabledहाँ करने के लिए सेट है, और यह पूरे सेल भरता है, तो आप सेल को छूने के लिए सुनने के लिए नहीं मिल सकता है। स्पर्श को जवाब देने के लिए सेल प्राप्त करने के लिए, आपको उपयोगकर्ता इंटरटेक्शन सक्षम टेक्स्ट फ़ील्ड के NO पर सेट करने की आवश्यकता है।

संपादित करें: और आप पाठ क्षेत्र संपादन योग्य बनाने के लिए करना चाहते हैं, जब सेल का चयन किया जाता है, तो निम्न कोड didSelectRowAtIndexPath में जोड़ें: विधि,

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

    // get the reference to the text field 
    [textField setUserInteractionEnabled:YES]; 
    [textField becomeFirstResponder]; 
} 
+0

आप निर्धारित करते हैं तो 'userInteractionEnabled = नहीं;' इस UITextField असंपादित प्रस्तुत करना नहीं होगा? –

+0

स्पष्ट रूप से नहीं: मैंने एम्स्टस्टैक सलाह का पालन किया है और सेट किया है [राशि फ़ील्ड setUserInteractionEnabled: NO]; DidSelectRow विधि की शुरुआत में, और फिर [राशि फ़ील्ड setUserInteractionEnabled: YES] तभी जब चयनित पंक्ति टेक्स्ट फ़ील्ड वाला है। चयन अब ठीक है, लेकिन मुझे वास्तव में टेक्स्ट दर्ज करने के लिए पंक्ति पर दो बार टैप करना होगा ... –

+0

@micpringle, ofcourse। टेक्स्ट फ़ील्ड सेल को भरने के रूप में उनके पास टेक्स्ट फ़ील्ड और सेल दोनों स्पर्श नहीं कर सकते हैं। – EmptyStack

6

आप नहीं विभाजित कर दिया है सबव्यू जोड़कर कुछ भी, इसके बजाय यूआईटीएक्स्टफाल्ड UITableViewCell से पहले स्पर्श को कैप्चर कर रहा है। आप UITextField के बाहर टैप करके इसका परीक्षण कर सकते हैं लेकिन UITableViewCell की सीमाओं के भीतर और आप देखेंगे कि यह वास्तव में अभी भी चयन करेगा जैसा आप उम्मीद करेंगे।

इसे पाने के लिए, आप UITextField को उपclass कर सकते हैं और UITableView प्रॉपर्टी जोड़ सकते हैं। जब आप UITextField को तुरंत चालू करते हैं और उसे सेल में जोड़ते हैं तो संपत्ति सेट करें।

amountField.tableView = tableView; 

तो फिर आप अपने उपवर्ग में becomeFirstResponder ओवरराइड करने के लिए आवश्यकता होगी, और विधि में UITextField के साथ सेल के लिए पंक्ति हो और फिर उसे चुनने के लिए मैन्युअल रूप से

- (BOOL)becomeFirstResponder 
{ 
    // Get the rect of the UITextView in the UITableView's coordinate system 
    CGRect position = [self convertRect:self.frame toView:self.tableView]; 
    // Ask the UITableView for all the rows in that rect, in this case it should be 1 
    NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position]; 
    // Then manually select it 
    [self.tableView selectRowAtIndexPath:[indexPaths objectAtIndex:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; 
    return YES; 
} 
+0

आप बिल्कुल सही हैं, मैंने कोशिश की, यह सच है। धन्यवाद। –

+0

यह भी काम करता है, लेकिन टेक्स्ट फ़ील्ड को सेट करते समय इसे बहुत अधिक अतिरिक्त कोड की आवश्यकता होती है IteractionEnabled = NO जैसा कि नीचे खाली सुझाव दिया गया है, कोड की कुछ पंक्तियां थी .... कम से कम जब तक मुझे कोई अन्य समस्या नहीं मिलती :) धन्यवाद आपकी मदद के लिए हालांकि, यह मुद्दा स्पष्ट बना दिया। –

+0

क्या यह एक सतत चक्र नहीं है? 'tableView' ->' visibleCells' -> 'राशि फ़ील्ड '->' तालिका दृश्य ' – jakenberg

1

पहली बात है havent पुन: प्रयोज्य कोशिकाओं का इस्तेमाल किया। जो कोडिंग आपने प्रदान की है वह बहुत मेमोरी का कारण बन जाएगी।

अगला बात यह है कि आप टेक्स्टफील्ड के अलावा क्षेत्र को छूकर पंक्ति का चयन कर सकते हैं।अपने प्रश्न के लिए

एक समाधान मुझे यकीन है कि यह काम करेगा नहीं हूँ

है अपने textfield प्रतिनिधि

UITableViewCell *cell = (UITableViewCell *)[textField superView]; 
cell.selected=YES; //I think this will call the didSelectRowATIndex; 

में। लेकिन एक कोशिश के लायक है।

+0

इस तथ्य के अलावा कि यह तालिका केवल 4 कक्ष है क्योंकि यह एक अच्छा इनपुट मास्क है, इसलिए स्मृति एक बड़ी समस्या नहीं होनी चाहिए, लेकिन आप क्यों कह रहे हैं कि मैंने पुन: प्रयोज्य कोशिकाओं का उपयोग नहीं किया है? मुझे लगता है मेने कर दिया। मैं अब आपके समाधान का प्रयास करने जा रहा हूं। धन्यवाद। –

+0

मुझे लगता है कि आप पुन: प्रयोज्य अवधारणा को गलत समझाते हैं .. यहां देखें http: //developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html ... वैसे भी 4 कक्ष ठीक है लेकिन साथ 10 से अधिक की एक पंक्ति आपको एक समस्या मिलेगी। – iPrabu

+0

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

1

सबसे पहले आपको सेलफॉररोएट इंडेक्सपैथ के लिए पुन: उपयोग करने के लिए उपयोग करने की आवश्यकता है, कारण यदि आप पुन: उपयोग नहीं करते हैं IDententifier है: जब आप ऊपर और नीचे स्क्रॉल करते हैं, तो यह हमेशा नए सेल और नए टेक्स्टफील्ड आवंटित करेगा ताकि आपको सेल == शून्य स्थिति है, तो संशोधित कोड यहाँ है:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *reuseIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 
if (cell==nil) { 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]; 

    UITextField *amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 
    [amountField setDelegate:self]; 
    [cell.contentView addSubview:amountField]; 
} 
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
[[cell textLabel] setText:@"Amount"]; 

return cell; 
} 

didSelect प्रतिनिधि विधि में आप इस

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [prevField resignFirstResponder]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIView *view = [[cell.contentView subviews] lastObject]; 
    if([view isKindOfClass:[UITextField class]]{ 
    currentField = (UITextField*)view; 
    } 
    [currentField becomeFirstResponder]; 
    prevField = currentField; 
} 
+0

दोस्तों, मैं कोको सर्वोत्तम प्रथाओं को पढ़ाने के आपके प्रयास की सराहना करता हूं, लेकिन मैंने वहां एकमात्र प्रासंगिक कोड डालने की कोशिश की, राशि हर समय तत्काल नहीं होती है, लेकिन केवल देखने पर लोड किया जाता है, जांचें! सेल वहां है लेकिन मैं छोड़ दिया गया क्योंकि प्रासंगिक नहीं है मेरे प्रश्न के लिए, और मेरी तालिका स्क्रॉल नहीं करती है क्योंकि यह केवल 4 पंक्तियां है। हालांकि आपकी बाकी टिप्पणियां बहुत उपयोगी हैं। धन्यवाद! –

1

तरह सेल के अंदर पंक्ति के लिए सूचकांक पथ पर इस लाइन जो बोल्ड में दी गई है जोड़ने के लिए, कर सकते हैं

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 

    amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 

    [amountField setDelegate:self]; 
    [[cell textLabel] setText:@"Amount"]; 

    **for (UIView *cellSubViews in cell.subviews) { 
      cellSubViews.userInteractionEnabled = NO; 
    }** 

    [cell addSubview:amountField]; 
    return cell; 
} 

इस कोशिश, यह काम करेंगे

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