1

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!

Clyde
  • 45
  • 7
  • Why is it a problem that a separate date picker is created for every cell? There is no need for you to create the picker inside `didSelect` method. It can be a property on the cell. – Sulthan Mar 25 '17 at 18:10

2 Answers2

1

Singleton is the right way (like with DateFormatters, that are heavy too but often reused):

class GlobalDatePicker: UIDatePicker {
    static let shared = GlobalDatePicker()
}

You'll use a single date picker instance in your code.

You can also use a private init(), to prevent callers from creating instances without going through the singleton.

class GlobalDatePicker: UIDatePicker {
    static let shared = GlobalDatePicker()
    private init() {}
    // Your methods that modify the picker here...
}

But you can also make a static variable in your controller:

static var datePicker: UIDatePicker = {
    let picker = UIDatePicker()
    picker.datePickerMode = .date
    return picker
}()
Oleh Zayats
  • 2,423
  • 1
  • 16
  • 26
0

You can just make your datePicker static like:

static let datePicker = UIDatePicker()

Then you don't need to go through singleton. But if you want singleton, you can go through this question where you can find multiple ways of having singleton behavior in Swift-3.

Community
  • 1
  • 1
nayem
  • 7,285
  • 1
  • 33
  • 51