0

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.

1

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//

    }
}


nathancy
  • 42,661
  • 14
  • 115
  • 137
mjk
  • 1
  • 1

1 Answers1

0

Your code is crashing because datePickerIndexPath is an optional value that is never set. Declaring it like this doesn't give any value to the variable:

var datePickerIndexPath: NSIndexPath?

Thus, the value of datePickerIndexPath is nil.

The solution would be to just get rid of that variable completely. Then, change this code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    ddn.setTitle("\(datePickerIndexPath!.row)", for: .normal)

}

To this:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    ddn.setTitle("\(indexPath.row)", for: .normal)

}

The didSelectRowAt method already gives you indexPath, which tells you the row that the user selected.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • thanks i tried but still the space that looks like first row of tableview still exists and now when i click the space, it shows '0'. i dont know where it comes from – mjk Jun 14 '19 at 00:21
  • @mjk It's because you called `setTitle` here: `ddn.setTitle("\(indexPath.row)", for: .normal)` and it causes the button to display "0". – Xcoder Jun 14 '19 at 00:41
  • @mjk In order to show the date, check out [this answer](https://stackoverflow.com/a/29502350/8560567). Basically, set the title not as the row number, but the actual date of your date picker. – Xcoder Jun 16 '19 at 14:50