iPhone 7
iPhone 7

Reputation: 1741

Navigate through textfields in UITableView swift

Trying to navigate through text fields in UITableView. Able to navigate through text fields on simple view but not getting how to navigate when text fields are in UITableView.

Below is the code tried so far:

func textFieldDidBeginEditing(textField: UITextField)
{

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

Please guide thanks.

Update:

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
}

Upvotes: 0

Views: 1553

Answers (3)

Janmenjaya
Janmenjaya

Reputation: 4163

Here is the code for Swift 2.1

Here I have taken SampleTableViewCell which is a custom cell in which i have created textField.

For the purpose I have just taken the tag based on cell, so while accessing it in the textField's delegate method, You can know the textField is for which cell.

Here is the code :

//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 : Here is the delegate for TextField

//UITextField Delegate

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

Here I have navigate to next textfield by clicking the return button of text field.

// 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;
}

Upvotes: 2

YanSte
YanSte

Reputation: 10839

Best way for me.

UITableViewCell

class CustomTableViewCell: UITableViewCell {
     var nextTextField: (() -> Void)?
}

UITableViewDataSource, UITableViewDelegate

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    cell.nextTextField = { in
        guard let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row + 1, section: 0)) as? CustomTableViewCell else {
            return
        }
        cell.textField.becomeFirstResponder()
    }
    return cell
}

UITextFieldDelegate

...
func textFieldDidBeginEditing(textfield: RCTextField) {
    if viewModel.items.count-1 == textfield.tag {
       textField.returnKeyType = UIReturnKeyType.done
    } else {
       textField.returnKeyType = UIReturnKeyType.next
    }
}
...

Upvotes: 0

Ahmad F
Ahmad F

Reputation: 31645

You should use the tag to check if the current textfield is the first responder.

Note: Swift 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
    }

Hope that helped.

Upvotes: 1

Related Questions