brownmamba
brownmamba

Reputation: 173

Allowing user to only type in next available text field in Swift

New to Swift and xcode here. I am trying to make a simple to-do planner using a tableview and tableview cells in swift.

I was wondering if there is any way to make it so that when a user clicks on any text field, the text field to respond should be the next available text field only.

enter image description here

For example in this photo, if a user clicks on like the 10th text field, the text field that should open up should only be the 4th one, the one after the "homework" cell.

Is this something simple to do, or am I way over my head here?

Any other tips are appreciated!

Upvotes: 0

Views: 86

Answers (2)

sbsr
sbsr

Reputation: 362

you can use textFieldShouldReturn from UITextFieldDelegate if dealing only with textFields

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField != lastTextField{
        self.lastTextField.becomeFirstResponder()
    }
    return true
}

or for table with custom cells :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row != lastItemRow + 1{
        let cellToFocus = tableView.cellForRowAtIndexPath(indexPath)
        cellToFocus.textField.becomeFirstResponder()
    }
}

Upvotes: 0

Faruk
Faruk

Reputation: 2449

You can try to use UITableView's didSelectRowAtIndexPath. You will be able know selected row's indexPath. I assume that you have a data as array which is being used to load tableView's data. In that didSelectRowAtIndexPath method you can simply check the indexPath of selected row and your data count. If they match then do nothing, If not you can use tableView.selectRowAtIndexPath(:_) to select the right cell -according to your example, the 4th one-. I hope it will help.

Upvotes: 1

Related Questions