2012-10-30 42 views
1

非常初學者的obj-c問題。在UITableView中切換UITextField單元格

我已將兩個部分http://uaimage.com/image/c6c9ca23分組爲TableView。在第一部分中我使用UITextField的自定義單元格。我需要實現輸入Acessory視圖鍵盤附加按鈕「下一個」,「上一個」(在第一部分中的文本字段之間切換)和「完成」(關閉鍵盤)http://uaimage.com/image/62f08045。 問題是:如何通過點擊輸入附件按鈕來實現在TableView第一部分的單元格中的文本字段之間切換的可能性? 我是否需要標記單元格或文本框,如果是這樣,當用戶點擊輸入附件按鈕時如何檢索它們的值?或者這是一個更好的方法來做到這一點?

謝謝,亞歷克斯

回答

1

我假定說u有3的UITextField即TXT,TXT1,TXT 2 tags 0 1和2;現在在.h文件中添加UITableViewCell *cell

編輯: 我們擺脫當前的tableView細胞的所有文本字段的引用添加此委託方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    cell = nil; 
    cell = (UITableViewCell *)[textField superView]; 
    return YES; 
} 

現在輸入附件上一個按鈕動作做到這一點:

-(IBAction)previousBtn:(id)sender 
{ 
    UITextField *txt = (UITextField*)[cell viewWithTag:0]; 
    UITextField *txt1 = (UITextField*)[cell viewWithTag:1]; 
    UITextField *txt2 = (UITextField*)[cell viewWithTag:2]; 
    if(txt.isFirstResponder) 
    { 
    [txt resignFirstResponder]; 
    [txt2 becomeFirstResponder]; 
    } 
    else if(txt1.isFirstResponder) 
    { 
    [txt1 resignFirstResponder]; 
    [txt becomeFirstResponder]; 
    } 
    else if(txt2.isFirstResponder) 
    { 
    [txt2 resignFirstResponder]; 
    [txt1 becomeFirstResponder]; 
    } 
} 

現在在輸入附件下一步按鈕操作請執行以下操作:

-(IBAction)nextBtn:(id)sender 
{ 
    UITextField *txt = (UITextField*)[cell viewWithTag:0]; 
    UITextField *txt1 = (UITextField*)[cell viewWithTag:1]; 
    UITextField *txt2 = (UITextField*)[cell viewWithTag:2]; 

    if(txt.isFirstResponder) 
    { 
    [txt resignFirstResponder]; 
    [txt1 becomeFirstResponder]; 
    } 
    else if(txt1.isFirstResponder) 
    { 
    [txt1 resignFirstResponder]; 
    [txt2 becomeFirstResponder]; 
    } 
    else if(txt2.isFirstResponder) 
    { 
    [txt2 resignFirstResponder]; 
    [txt becomeFirstResponder]; 
    } 
} 
+0

如果我理解了這段代碼,我需要以編程方式編寫所有文本字段的屬性? – Alex

+1

不需要從tableView中的當前UITableViewCell獲取所有txt,txt1,txt2的引用 –

+1

查看編輯答案.... –