需求如下:
- 有開(kāi)始時(shí)間、結(jié)束時(shí)間勾哩、提醒時(shí)間三個(gè)cell需要彈出時(shí)間選擇器
- 開(kāi)始時(shí)間和結(jié)束時(shí)間只顯示日期死嗦,提醒時(shí)間顯示日期和時(shí)間(小時(shí)和分鐘)
- 選擇好時(shí)間好,開(kāi)始時(shí)間和結(jié)束時(shí)間所在的cell顯示文案只有年月日月劈,提示時(shí)間所在cell顯示的文案有年月日小時(shí)分鐘
- 編輯提醒時(shí)間時(shí)(之前已經(jīng)設(shè)置過(guò)提醒時(shí)間了,現(xiàn)在想改這個(gè)提醒時(shí)間)藤乙,打開(kāi)DatePicker的時(shí)候猜揪,DatePicker的當(dāng)前日期需要是之前設(shè)定的日期,不能是現(xiàn)在的日期
實(shí)現(xiàn)方法如下:
func updateDateLabel(indexPath:NSIndexPath) {
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
switch indexPath.row {
case 6 :
formatter.timeStyle = .ShortStyle
dueDateLabel.text = formatter.stringFromDate(dueDate)
case 2:
startDateLabel.text = formatter.stringFromDate(startDate)
case 3:
endDateLabel.text = formatter.stringFromDate(endDate)
default:
print("erro.....")
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row {
case 2:
showDatePicker(indexPath)
case 3:
showDatePicker(indexPath)
case 6:
showDatePicker(indexPath)
default:
print("點(diǎn)擊了其他行")
}
}
func showDatePicker(indexPath: NSIndexPath) {
let alert = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n\n\n\n\n\n", preferredStyle: .ActionSheet)
let cancel = UIAlertAction(title: "取消", style: .Cancel , handler: nil)
let datePicker = UIDatePicker()
switch indexPath.row {
case 2:
datePicker.datePickerMode = .Date
datePicker.setDate(startDate, animated: false)
case 3:
datePicker.datePickerMode = .Date
datePicker.setDate(endDate, animated: false)
case 6:
datePicker.datePickerMode = .DateAndTime
datePicker.setDate(dueDate, animated: false)
default:
print("點(diǎn)擊了其他行")
}
alert.view.addSubview(datePicker)
let confirmOk = UIAlertAction(title: "確定", style: .Destructive) { (UIAlertAction) in
switch indexPath.row {
case 6: self.dueDate = datePicker.date
case 2: self.startDate = datePicker.date
case 3: self.endDate = datePicker.date
default: print("erro")
}
self.updateDateLabel(indexPath)
}
alert.addAction(cancel)
alert.addAction(confirmOk)
presentViewController(alert, animated: true, completion: nil)
}