【问题标题】:Detect when user is done typing in a UITextField within a UITableViewCell检测用户何时在 UITableViewCell 中输入 UITextField
【发布时间】:2015-12-20 22:33:21
【问题描述】:

我在UITableViewCell 中嵌入了一个UITextField。我正在尝试实现类似于 Instagram 评论的功能,当用户在文本字段中有文本时,发送按钮被启用,如果没有文本,则发送按钮未被启用。即使实现了textFieldDidEndEditing,我似乎也无法检测到用户何时完成输入。此方法似乎仅在单击返回按钮时才有效。有没有一种方法可以检测用户何时在文本字段中完成输入并且至少有一个字符,从而启用发送按钮?

我的代码:

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()
         }
     }
 }

【问题讨论】:

    标签: ios swift uitableview uitextfield swift2


    【解决方案1】:

    为您的textField 添加操作EditingChanged,只要您向textField 添加值,就会调用该操作。

    更新 我在情节提要中添加了一个 textField 和一个按钮。我创建了两个出口,一个用于按钮,一个用于 textField,并为我的 textField (EditingChanged) 添加了一个操作。

    您现在唯一需要做的是启用/禁用按钮:

    @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
            }
        }
    

    【讨论】:

    • 代替textFieldDidEndEditing?
    • 如果你愿意,你可以同时拥有这两个,但我认为 EditingChanged 就足够了,所以如果你不需要另一个,可以替换它们。
    • 当我从文本字段中删除字符时抛出错误(可能太快以至于方法跟不上?):Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 3, but there are only 3 sections before the update'
    • 实际上,当我尝试从文本字段中删除最后一个字符时,它会引发该错误
    • 奇怪...您是否从故事板的代码中删除了旧操作?尝试从您的代码和情节提要中删除所有操作,然后为您的 textField 添加 EditingChanged 操作。我刚试过,很适合我。尝试在其中添加 EditingChanged 和一个 print("Changed") 并检查它是否有效,然后添加您的代码。
    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 2011-01-26
    • 2020-12-21
    • 1970-01-01
    • 2012-06-04
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多