按TabView单元格滑动编辑操作后如何显示日期选择器

时间:2017-11-15 15:56:11

标签: ios uitableview swift3 datepicker

我想在按下datepicker后显示tableview swipe edit action button。它被称为推迟,在打开一个datepicker后,我希望我的用户选择一个日期并按下datepicker工具栏的完成按钮。我已经创建了datepickertoolbar,但我不知道如何通过编辑操作来展示它。

以下是日期选择代码:

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)
}

编辑操作代码:

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]
}

修改操作按钮图片:

Edit Action buttons

其他页面中的Datepicker图片:

Datepicker

  

更新〜我添加此代码后

picker.addSubview(toolbar)
    taskTableView.addSubview(picker)
    taskTableView.bringSubview(toFront: picker)

Datepicker

1 个答案:

答案 0 :(得分:1)

您可以显示将datePicker添加到视图中的日期:

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()