wxcoder
wxcoder

Reputation: 655

Detect when user is done typing in a UITextField within a UITableViewCell

I have a UITextField embedded within a UITableViewCell. I am trying to implement a feature similar to Instagram's commenting where when the user has text in the textfield the send button is enabled, and if there is no text the send button is not enabled. I can't seem to detect when the user is done typing even when having implemented textFieldDidEndEditing. This method only seems to work if the return button is clicked. Is there a way to detect when the user is done typing in the textfield and has at least one character, thereby enabling the send button?

My code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
 ...
case .comment:
            let cell = tableView.dequeueReusableCellWithIdentifier(cellInfo.description, forIndexPath: indexPath) as! PictureInformationTableViewCell
            cell.sendMessageBtn.addTarget(self, action: "didTapSendMessageBtn:", forControlEvents: .TouchUpInside)
            cell
            cell.commentTextField.layer.borderWidth = 1
            cell.commentTextField.layer.borderColor = UIColor.whiteColor().CGColor
            cell.sendMessageBtn.layer.cornerRadius = 5
            if initBtn == false {
                cell.sendMessageBtn.enabled = false
            }
            buttonColor = cell.sendMessageBtn.tintColor
            cell.commentTextField.delegate = self
            //cell.commentTextField.addTarget(self, action: "didEndEditing:", forControlEvents: UIControlEvents.e)
            return cell
        }


func textFieldDidEndEditing(textField: UITextField) {
     let cell = tableView.dequeueReusableCellWithIdentifier(PictureInformation.comment.description, forIndexPath: NSIndexPath(forRow: 0, inSection: 3)) as! PictureInformationTableViewCell
     if let text = textField.text {
         initBtn = true
         if text.characters.count > 0 {
             cell.sendMessageBtn.enabled = true
             cell.sendMessageBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
             tableView.reloadInputViews()
         } else {
             cell.sendMessageBtn.enabled = false
             cell.sendMessageBtn.setTitleColor(buttonColor, forState: UIControlState.Normal)
             tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 3)], withRowAnimation: .None)
             tableView.reloadInputViews()
         }
     }
 }

Upvotes: 0

Views: 1489

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

Add the action EditingChanged for your textField that action is called whenever you add a value to your textField.

Update I have added a textField and a button in my storyboard. I have created two outlets, one for the button and one for the textField and I have added an action for my textField (EditingChanged).

The only thing you need to do now to enabled/disable the button is:

@IBOutlet weak var btn: UIButton!
@IBOutlet weak var txtField: UITextField!

@IBAction func txtField_EditingChanged(sender: AnyObject) {
        if txtField.text?.characters.count > 0{
            btn.enabled = true
        }
        else{
            btn.enabled = false
        }
    }

Upvotes: 1

Related Questions