Hello, I'm trying to display/hide a UIDatePicker embedded in a UITableViewCell. My intention is that the datePicker will start hidden. Then when the user selects the startDate label in the cell above the date picker, the cell's row hight will adjust, making the datePicker visible. Then when the user taps the startDate label again, the datePicker hides. They should be able to do this indefinitely.
In this code, the datePicker starts out hidden (as intended), shows when the user selects the StartDate label (as intended) and then hides when the user selects the startDate label again (as intended). However, if the user tries to show the datePicker for a 2nd time, nothing happens.
video link: https://youtube.com/shorts/blu1OCBOCiE?feature=share
class AddEditLogbookTableViewController: UITableViewController {
@IBOutlet var nameTextField: UITextField!
@IBOutlet var descriptionTextField: UITextField!
@IBOutlet var startDateLabel: UILabel!
@IBOutlet var startDateDatePicker: UIDatePicker!
var logbook: Logbook?
//Couple variables related to displaying/hiding the date picker
var startDatePickerCellIndexPath = IndexPath(row: 3, section: 0)
var startDateLabelCellIndexPath = IndexPath(row: 2, section: 0)
var isStartDatePickerVisible: Bool = false {
didSet {
startDateDatePicker.isHidden = !isStartDatePickerVisible
}
}
init?(coder: NSCoder, logbook: Logbook?) {
self.logbook = logbook
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
updateDateView()
}
@IBAction func startDatePickerValueChanged(_ sender: UIDatePicker) {
updateDateView()
}
func updateDateView() {
var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
return dateFormatter
}()
startDateLabel.text = dateFormatter.string(from: startDateDatePicker.date)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath {
case startDatePickerCellIndexPath:
if !isStartDatePickerVisible {
print("indexPath: \(indexPath) row height: 0 if")
return 0
}
else {
print("indexPath: \(indexPath) row height: \(UITableView.automaticDimension) if/else")
return UITableView.automaticDimension }
default:
print("indexPath: \(indexPath) row height: \(UITableView.automaticDimension) default")
return UITableView.automaticDimension
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath == startDateLabelCellIndexPath {
isStartDatePickerVisible.toggle()
print("isStartDatePickerVisible Toggled. Value: \(isStartDatePickerVisible)")
} else { return }
tableView.beginUpdates()
tableView.endUpdates()
}
}
The print statements output as follows.
Tap 1:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 3] row height: -1.0 if/else
indexPath: [0, 3] row height: -1.0 if/else
Tap 2:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 3] row height: 0 if
Tap 3:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
Tap 4:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default