I have two textFields
and a TableView
. The Tableview
should initially be disabled (meaning the labels
greyed out and the cells
are not clickable). When both textFields
have values, I want the TableView
and the tableViewCells
to be clickable and not greyed out.
So far, I've added targets to both of the textfields
to track when the user is editing:
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
}
Then I created an extension for UITableViewCell
that basically "enables" and "disables" the tableView
(retrieved from this thread):
extension UITableViewCell {
func enable(on: Bool) {
for view in contentView.subviews {
view.isUserInteractionEnabled = on
view.alpha = on ? 1 : 0.5
}
}
}
Finally, I implemented this method as well as the variable isUserEditing
in my 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
}
The tableView
appears "disabled" when the view loads, however when I start typing in one or both of the textFields
, it remains disabled. So obviously the viewForHeaderInSection
isn't reloading, or being executed again.
Is there any way to achieve this? Can I run the viewForHeaderInSection
in realtime and update automatically? Any help is appreciated, thanks!
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