为什么我的DatePicker不隐藏?

时间:2016-11-20 05:10:25

标签: ios swift datepicker

每当我点击日期选择器上方的行时,我的日期选择器就不会像它应该的那样隐藏。而是当我点击该行时,它会永久性地突出显示为深灰色,这在您最初点击某些内容时很常见。为什么会这样?为什么我的datePicker不隐藏?

class TableViewController: UITableViewController {

@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
private var datePickerHidden = false



override func viewDidLoad() {
    super.viewDidLoad()
    datePickerChanged()
    self.tableView.backgroundColor = UIColor.lightGray

}

@IBAction func valueChanged(_ sender: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .short
    detailLabel.text = formatter.string(from:sender.date)
}

func datePickerChanged () {
    detailLabel.text = DateFormatter.localizedString(from: datePicker.date, dateStyle: DateFormatter.Style.short, timeStyle: DateFormatter.Style.short)
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {
        toggleDatepicker()
    }
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}

private func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if !datePickerHidden && indexPath.row == 1 {
        return 0
    }
    else {
        return 100
    }
}

private func toggleDatepicker() {

    datePickerHidden = !datePickerHidden

    tableView.beginUpdates()
    tableView.endUpdates()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

像这样更新你的代码:

datePickerHidden = !datePickerHidden 
datePicker.hidden = datePickerHidden