我想在按下 tableview 滑动编辑操作按钮
后显示一个 datepicker
。它被称为推迟,在打开日期选择器后,我希望我的用户选择一个日期并按下日期选择器工具栏的完成按钮。我已经创建了 datepicker
和 toolbar
但我不知道如何通过编辑操作显示它。
这是日期选择器代码:
func createDatePicker() {
picker.datePickerMode = .date
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
toolbar.tintColor = UIColor(red: 189/255, green: 194/255, blue: 201/255, alpha: 1)
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
doneButton.tintColor = UIColor.black
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelPicker))
cancelButton.tintColor = UIColor.black
toolbar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolbar.isUserInteractionEnabled = true
//dateTimeTextField.inputAccessoryView = toolbar (I was using this part to open a datepicker with a text field but I don't know how to modify it for the edit action)
//dateTimeTextField.inputView = picker
picker.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
}
编辑 Action 代码:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
let postpone = UITableViewRowAction(style: .normal, title: "Postpone"){(UITableViewRowAction,NSIndexPath) -> Void in
self.tasks[indexPath.row].status = "postpone"
let encodedData = NSKeyedArchiver.archivedData(withRootObject: self.tasks)
UserDefaults.standard.removeObject(forKey: "taskList")
UserDefaults.standard.set(encodedData, forKey: "taskList")
self.taskTableView.reloadData()
}
postpone.backgroundColor = UIColor.init(red: 222/255, green: 119/255, blue: 49/255, alpha: 1)
return [done, cancel, postpone]
}
编辑操作按钮图像:
不同页面中的日期选择器图像:
Update ~ After I added this code
picker.addSubview(toolbar)
taskTableView.addSubview(picker)
taskTableView.bringSubview(toFront: picker)
最佳答案
您可以显示将其添加到 View 中的日期选择器:
func createDatePicker() {
picker.datePickerMode = .date
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
toolbar.tintColor = UIColor(red: 189/255, green: 194/255, blue: 201/255, alpha: 1)
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
doneButton.tintColor = UIColor.black
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelPicker))
cancelButton.tintColor = UIColor.black
toolbar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolbar.isUserInteractionEnabled = true
picker.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
//add the picker to your view or tableView if you use UITableViewController
taskTableView.addSubview(picker)
taskTableView.bringSubview(toFront: picker)
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let postpone = UITableViewRowAction(style: .normal, title: "Postpone"){(UITableViewRowAction,NSIndexPath) -> Void in
self.tasks[indexPath.row].status = "postpone"
let encodedData = NSKeyedArchiver.archivedData(withRootObject: self.tasks)
UserDefaults.standard.removeObject(forKey: "taskList")
UserDefaults.standard.set(encodedData, forKey: "taskList")
self.taskTableView.reloadData()
//show your date picker
self.createDatePicker()
}
postpone.backgroundColor = UIColor.init(red: 222/255, green: 119/255, blue: 49/255, alpha: 1)
return [done, cancel, postpone]
}
删除选择器调用:picker.removeFromSuperview()
关于ios - 如何在按下 TableView 单元格滑动编辑操作后显示日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311892/