Search code examples
swiftuitableviewkeyboarduitextfieldinputview

Setup inputView for different UITextFields in UITableView


I have UITableView with UITextFields in custom cells.

For UITextField I use custom inputView:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! Item

        cell.price.text = ""
        cell.price.delegate = self

        return cell
    }

func textFieldDidBeginEditing(textField: UITextField) {
        textField.inputView = keyboardView
        itemPrice = textField
    }

func numberWasTapped(number: String!) {
        itemPrice.insertText(number!)
    }

With delegate method:

@IBAction func keyTaped(sender: UIButton) {
      switch sender.tag {
        case 112:
          if !isCommaWasPressed {
             isCommaWasPressed = true
             sender.enabled = false
             self.delegate?.numberWasTapped(sender.titleLabel!.text)
           }
           break
       default:
           self.delegate?.numberWasTapped(sender.titleLabel!.text)
       }

My custom inputView is the calculator keyboard with the all logic in the Keyboard class: it controls if comma was pressed and how many numbers was entered after the comma. For example, if comma was pressed, then commaButton.enabled = false in Keyboard class.

I couldn't figure out how to set the commaButton.enabled = true in Keyboard class when I'm switching to another UITextField.

I tried textField.reloadInputViews() in textFieldDidBeginEditing(_:) method but it doesn't work.

I think, I should get the information from UITextField and pass it to Keyboard's class logic. But I don't know how to do it.


Solution

  • I found the solution. I moved keyboard logic to main ViewController and no need to pass textField data to Keyboard class.