【发布时间】: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