我有两个textFields
和一个TableView
。 Tableview
最初应被禁用(这意味着labels
变灰并且cells
不可点击)。当两个textFields
都具有值时,我希望TableView
和tableViewCells
是可单击的并且不灰显。
到目前为止,我已经向两个textfields
添加了目标,以跟踪用户何时进行编辑:
var isUserEditing: Bool = false
func setup() {
mealItemTextField.addTarget(self, action: #selector(userIsEditing), for: UIControl.Event.editingChanged)
priceTextField.addTarget(self, action: #selector(userIsEditing), for: UIControl.Event.editingChanged)
}
@objc func userIsEditing(sender: UITextField) {
sender.text = sender.text?.trimmingCharacters(in: .whitespaces)
guard
let mealtext = mealItemTextField.text, !mealtext.isEmpty,
let pricetext = priceTextField.text, !pricetext.isEmpty
else
{
isUserEditing = false
return
}
isUserEditing = true
}
然后,我为UITableViewCell
创建了一个扩展名,该扩展名基本上“启用”和“禁用”了tableView
(从this线程检索):
extension UITableViewCell {
func enable(on: Bool) {
for view in contentView.subviews {
view.isUserInteractionEnabled = on
view.alpha = on ? 1 : 0.5
}
}
}
最后,我在isUserEditing
中实现了该方法以及变量viewForHeaderInSection
:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let userModel = Data.userModels[section]
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell") as! NameHeaderTableViewCell
cell.setup(model: userModel)
cell.enable(on: false)
if isUserEditing == true {
cell.enable(on: true)
}
return cell.contentView
}
在加载视图时,tableView
会显示为“禁用”,但是当我开始输入textFields
中的一个或两个时,它将保持禁用状态。因此很明显viewForHeaderInSection
不会重新加载或再次执行。
有什么办法可以做到这一点?我可以实时运行viewForHeaderInSection
并自动更新吗?感谢您的任何帮助,谢谢!
This should be the initial view when there is no text in the textField
This should be the view after there is some text in both textFields
答案 0 :(得分:8)
tableView.reloadData()丢失,因为未重新加载该表视图。
@objc func userIsEditing(sender: UITextField) {
sender.text = sender.text?.trimmingCharacters(in: .whitespaces)
guard
let mealtext = mealItemTextField.text, !mealtext.isEmpty,
let pricetext = priceTextField.text, !pricetext.isEmpty
else
{
isUserEditing = false
tableView.reloadData() // reload table view here
return
}
isUserEditing = true
tableView.reloadData() // reload table view here
}
答案 1 :(得分:1)
如果您希望在出现键盘时启用tableView并且文本字段为空,则可以通过通知进行检查;
在您的ViewController类中:
override func viewDidLoad() {
super.viewDidLoad()
yourTableView.isUserInteractionEnabled = false
// check notifications if keyboard shown or not
NotificationCenter.default.addObserver(self, selector: #selector(showKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(hideKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
// if your textFields out of your tableview
yourTextField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
yourTextField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
}
@objc func textFieldDidChange(textField: UITextField){
self.yourTableView.isUserInteractionEnabled = true
print("Text changed")
}
// show keyboard function
@objc func showKeyboard(notification: NSNotification){
// You can disable your tableview or your cells in this case i just disable tableview.
yourTableView.isUserInteractionEnabled = true
print("keyboard appeared")
}
// this part is up to how you want to implement. If you want to keep tableview active after textField edited don't use this method
@objc func hideKeyboard(notification: NSNotification){
// You can disable your tableview or your cells
yourTableView.isUserInteractionEnabled = false
print("keyboard disappeared")
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let userModel = Data.userModels[section]
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell") as! NameHeaderTableViewCell
// if your text Fields inside tableView tracking you textfield changes (in this case you couldn't disable tableView initially)
cell.yourTextField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
cell.yourTextField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
return cell.contentView
}
我希望这能解决您的问题,祝您好运。