2012-01-16 21 views
24

मैं गतिशील रूप से UITextField के साथ तालिका दृश्य बना रहा हूं।तालिका में UITextField पर फ़ोकस सेट करें

l_textField = [[UITextField alloc] initWithFrame:TextFieldFrame]; 
l_textField.tag = cellRow; 

l_textField.delegate = self; 
l_textField.font = [UIFont boldSystemFontOfSize:14]; 
l_textField.textColor = [UIColor blackColor]; 
[l_textField setEnabled:YES]; 

[cell.contentView addSubview:l_textField]; 

और अब मैं उपयोगकर्ता टेक्स्ट सेल पर इस टेक्स्ट फ़ील्ड पर फ़ोकस सेट करना चाहता हूं। मैं इस

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath { 
    UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row]; 
    [field resignFirstResponder]; 
} 

लिखने लेकिन यह काम नहीं करता

उत्तर

59

परिवर्तन [field resignFirstResponder];[field becomeFirstResponder];

resignFirstResponder साथ पाठ क्षेत्र से कुंजीपटल (फोकस) को हटाने के

+0

कुछ भी नहीं happend कहा जाता है, मैं ऐसा लगता है क्योंकि मैं सेट tblView.allowsSelection = नहीं; और यह घटना खुश नहीं है। लेकिन अगर मैं tblView.allows चयन = हाँ सेट करता हूं; यह केवल सेल का चयन करता है = ( – nabiullinas

+0

मैंने इस तरह के दृष्टिकोण को लागू करने की कोशिश की, हालांकि यह आमतौर पर काम नहीं करता है। UITextField को [textField बनने के लिए फर्स्ट रेस्पॉन्डर] को असाइन करने की कोशिश कर रहा है, चयन को सेट करना होगा या YES/true पर सेट करना होगा। –

1

प्रयोग किया जाता है मैं एक ही सामना करना पड़ा गया है यहां समस्याएं, यहां तक ​​कि [field becomeFirstResponder]; का सही उपयोग भी करें।

मैंने सेल के अंदर मौजूद वस्तुओं को प्राप्त करने के लिए टैग का भी उपयोग किया। आपका didSelectRowAtIndexPath सही सेल प्राप्त करने के लिए इस तरह दिखना चाहिए:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    currentRow = indexPath.row; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UITextField *indexField = (UITextField *)[cell viewWithTag:101]; 
    [indexField becomeFirstResponder]; 
} 

यह मेरे लिए एक आकर्षण की तरह काम किया।

0

स्विफ्ट 3

myTextField.becomeFirstResponder() 

मेरे कोड। वर्तमान सेल के सूचकांक पथ ले लो। और अगले indexpath.row का सेल प्राप्त करें। और ** cell1.txtFindings.becomeFirstResponder() **

if let textFieldIndexPath = specsListView.indexPath(for: cell){ 
     let newIndexPath = IndexPath(row: textFieldIndexPath.row + 1, section: 0) 

     if let cell1: SpecsTableViewCell = specsListView.cellForRow(at: newIndexPath) as? SpecsTableViewCell{ 
      cell1.txtFindings.becomeFirstResponder() 
     } 
} 
संबंधित मुद्दे