2016-10-19 10 views
-1

UITableViewのテキストフィールドをナビゲートしようとしています。テキストフィールドが単純なビューではナビゲートできますが、テキストフィールドがUITableViewの場合はナビゲート方法がわかりません。UITableViewのテキストフィールドをナビゲートする

func textFieldDidBeginEditing(textField: UITextField) 
{ 

    //textField.inputAccessoryView = numberToolbar 
    if(textField == txtConfirmPassword) 
    { 
     textField.returnKeyType = UIReturnKeyType.Done 
    } 
    else 
    { 
     textField.returnKeyType = UIReturnKeyType.Next 
    } 
} 

案内してください感謝:以下

は、これまでに試したコードです。

更新:

func textFieldDidBeginEditing(textField: UITextField) 
{ 
    if(delegate != nil) 
    { 
     delegate?.performSelector(NSSelectorFromString("editingTextField")) 
    } 

    if(textField.tag == 3) 
    { 
     textField.returnKeyType = UIReturnKeyType.Done 
    } 
    else 
    { 
     textField.returnKeyType = UIReturnKeyType.Next 
    } 
} 

func textFieldShouldReturn(textField: UITextField) -> Bool 
{ 
    if(delegate != nil) 
    { 
     delegate?.performSelector(NSSelectorFromString("editingDone")) 
    } 
    if(textField.tag == 3) 
    { 
     textField.resignFirstResponder() 
     delegate?.performSelector(NSSelectorFromString("editingDone")) 

    } 

    let nextTage=textField.tag+1 
    // Try to find next responder 
    let nextResponder=textField.superview?.viewWithTag(nextTage) as UIResponder! 

    if (nextResponder != nil){ 
     // Found next responder, so set it. 
     nextResponder?.becomeFirstResponder() 
    } 
    else 
    { 
     // Not found, so remove keyboard 
     textField.resignFirstResponder() 
    } 
    return true 
} 

答えて

0

あなたが現在textfieldが最初の応答者であるかどうかを確認するためにtagを使用する必要があります。

注:スウィフト助け3.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // let's say that you are reading the date from an array... 
     return myArray.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell 

     // here you set the tag 
     cell.myTextfield?.tag = indexPath.row 

     // ... 

     return cell 
    } 


    func textFieldDidBeginEditing(textField: UITextField) { 
     // now, based on textField you can check (tag == myArray.count - 1 means it is the last) 
     textField.returnKeyType = (textField.tag == myArray.count - 1) ? .done : .next 
    } 

希望。ここで

+0

スウィフト2を投稿できますか? –

+3

!それをSwift 2に変更するにはあまりにも怠惰ではない:)あなたがしなければならないのは、tableViewのdataSourceメソッド名を変更することだけで、あなたの問題を解決できます。 –

2

スイフト2.1ここ

ためのコードである私は私がTextFieldを作成しているカスタムセルであるSampleTableViewCellをとっています。

私はちょうどセルに基づいてタグを取りました。だから、textFieldのデリゲートメソッドでそれにアクセスしている間に、どのセルのtextFieldがあるか分かります。

//UITableView Delegate 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SampleTableViewCell 
    cell.txtFld.delegate = self; 
    cell.txtFld.tag = indexPath.row + 100 

    cell.selectionStyle = UITableViewCellSelectionStyle.Gray; 


    return cell; 
} 

UPDATE:ここ

はコードですここで私は、テキストフィールドの戻るボタンをクリックして次のテキストフィールドに移動していTextFieldの

ここ
//UITextField Delegate 

func textFieldDidBeginEditing(textField: UITextField) 
{ 

    //textField.inputAccessoryView = numberToolbar 
    if(textField.tag == 100) 
    { 
     textField.returnKeyType = UIReturnKeyType.Done 
    } 
    else 
    { 
     textField.returnKeyType = UIReturnKeyType.Next 
    } 
} 

ためのデリゲートです。

// called when 'return' key pressed. return NO to ignore. 
func textFieldShouldReturn(textField: UITextField) -> Bool 
{ 
    textField.resignFirstResponder(); 

    if(textField.tag == 100) 
    { 
     let txtFld = self.tblList.viewWithTag(101); 
     txtFld?.becomeFirstResponder(); 
    } 

    return true; 
} 

希望します。

ハッピーコーディング...

+0

これは、テキストフィールドの代理人を呼び出すのではなく、delegate = selfを実装しており、上記のUITextFieldDelegate –

+0

を持っています。UITextFieldDelegateをビューコントローラに含めましたか?デリゲートをcellForRowAtIndexに設定したことを確認してください。だから間違いなくそれを呼び出す必要があります。私はこれをすでにチェックしている。ちょうどそこにチェックして私に知らせてください – Janmenjaya

+0

問題はありません、私のエラーデリゲートは、カスタムセルクラスで動作します。 –

関連する問題