I have a UIDatePicker and based on my code I believe that each time a cell in a table view is selected the date picker is re-created or something similar to the date picker being re-created occurs. I have looked into something called a singleton but I am confused about they work because I am new to swift. Would a singleton prevent this problem? If so how would I implement one into my code. If not how do I prevent this issue from occurring? Here is my code for the function I use to call createDatePicker and the function createDatePicker:
let datePicker = UIDatePicker()
func createDatePicker(indexPath: IndexPath, textField: UITextField){
//Tool Bar
let toolbar = UIToolbar()
toolbar.sizeToFit()
//Bar Button Item
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: nil)
toolbar.setItems([doneButton], animated: true)
textField.inputAccessoryView = toolbar
textField.inputView = datePicker
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if(indexPath.row != 0){
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell2
cell.textField.isUserInteractionEnabled = true
cell.textField.delegate = self
if(indexPath.row == 2 || indexPath.row == 3){
createDatePicker(indexPath: indexPath, textField: cell.textField)
}
cell.textField.becomeFirstResponder()
if(indexPath.row == 1){
cell.textField.placeholder = "Event Title"
} else if(indexPath.row == 2){
cell.textField.placeholder = "Start Time"
} else if(indexPath.row == 3){
cell.textField.placeholder = "End Time"
}
}
}
Thank you for any help in advance!