i made a show/hide date picker on a button click and show the date on the button, using Tableview. but when i run this, there's weird blank space between inside of tableview and datepicker. when i click this space, there's an error
'Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value'
i thought it's because i set the prototype cells : 1. so i made it 0 but didnt work either.
var dateFormatter = DateFormatter()
var datePickerIndexPath: NSIndexPath?
var datepicker : UIDatePicker?
@IBOutlet weak var ddn: UIButton!
@IBOutlet weak var TV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
TV.isHidden=true
datepicker = UIDatePicker()
datepicker? = UIDatePicker.init(frame: CGRect(x:0, y:40, width:252, height:150))
datepicker?.setValue(UIColor(red:0.95, green:0.92, blue:0.90, alpha:1.0), forKeyPath: "textColor")
datepicker?.datePickerMode = .date
datepicker?.addTarget(self, action: #selector(datepickerViewController.dateChanged(datepicker: )),for: .valueChanged)
TV.addSubview(datepicker!)
}
...
button click action/animate
...
//talbe view part//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rows = 1
if datePickerIndexPath != nil {rows += 1 }
return rows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DatePickerCell", for: indexPath)
cell.textLabel?.text = dateFormatter.string(from: (datepicker?.date)!)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
ddn.setTitle("\(datePickerIndexPath!.row)", for: .normal)
//when i click the space, an error occurs in this part//
}
}