Search code examples
swiftuitableviewtableviewuidatepicker

swift uidatePicker pass as arguements not working properly


i am stuck in situation i have to pass a uidatepicker to a cell class. the cell class contains 14 textfields and datepicker will be used as inputview to select to and from time for whole week. the thing is when i directly initialize it in cell class. it gets initialized 14 times and thats too much costly task. but while passing as arguement it replicate the selected time to all the textfield instead of that specific one. i tried to use tags and some other ways as well but no luck. how to handle the situation that it will not impact applicatio performancee .any help regarding this will be appriciated.thankyou

Code:

lazy var fromDatePicker: UIDatePicker = {
     let fromDatePicker = UIDatePicker()
    fromDatePicker.datePickerMode = .time
    fromDatePicker.locale = NSLocale(localeIdentifier: "en_US") as Locale
    fromDatePicker.minuteInterval = 30
    if #available(iOS 13.4, *) {
        fromDatePicker.preferredDatePickerStyle = .wheels
        
    } else {
        // Fallback on earlier versions
    }
     return fromDatePicker
 }()

cell.configureCellForEmployee(with:viewModel.profileDataSource, index: indexPath.row, fromDatePicker: fromDatePicker)     

Solution

  • Try adjusting the code.

    class "you class": UIViewController{
    
        lazy var fromDatePicker: UIDatePicker = {
             let fromDatePicker = UIDatePicker()
            fromDatePicker.datePickerMode = .time
            fromDatePicker.locale = NSLocale(localeIdentifier: "en_US") as Locale
            fromDatePicker.minuteInterval = 30
            if #available(iOS 13.4, *) {
                fromDatePicker.preferredDatePickerStyle = .wheels
                
            } else {
                // Fallback on earlier versions
            }
             return fromDatePicker
         }()
    }