我在Swift中有一个Table View Controller项目。 我需要2个本地通知操作:一个完成并删除截止日期待办事项,另一个打开应用程序的视图控制器。 我已经为动作添加了NSNotificationCenter.defaultCenter().addObserver
,但我仍然想知道如何删除cellForRowAtIndexPath
中的待办事项。只使用通知操作。 我希望你能帮助我!先谢谢你!如何使本地通知操作直接删除Swift中的待办事项
这里是我的代码一些地方:
的AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Actions
var firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
firstAction.identifier = "FIRST_ACTION"
firstAction.title = "Complete" // "First Action"
firstAction.activationMode = UIUserNotificationActivationMode.Background
firstAction.destructive = true
firstAction.authenticationRequired = false
var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
secondAction.identifier = "SECOND_ACTION"
secondAction.title = "Edit" // "Second Action"
secondAction.activationMode = UIUserNotificationActivationMode.Foreground
secondAction.destructive = false
secondAction.authenticationRequired = false
var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
thirdAction.identifier = "THIRD_ACTION"
thirdAction.title = "Third Action"
thirdAction.activationMode = UIUserNotificationActivationMode.Background
thirdAction.destructive = false
thirdAction.authenticationRequired = false
// category
var firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
firstCategory.identifier = "FIRST_CATEGORY"
let defaultActions:NSArray = [firstAction, secondAction, thirdAction]
let minimalActions:NSArray = [firstAction, secondAction]
firstCategory.setActions(defaultActions as! [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
firstCategory.setActions(minimalActions as! [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)
// NSSet of all our categories
let categories:NSSet = NSSet(objects: firstCategory)
let types:UIUserNotificationType = UIUserNotificationType(arrayLiteral: .Alert, .Badge)
let mySettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as! Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
func application(application: UIApplication!,
handleActionWithIdentifier identifier:String!,
forLocalNotification notification:UILocalNotification!,
completionHandler: (() -> Void)!){
if (identifier == "First_Action"){
NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)
}else if (identifier == "Second_Action"){
NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)
}
completionHandler()
ToDoTableViewController:
//
NSNotificationCenter.defaultCenter().addObserver(self, selector: "drawAShape", name: "actionOnePressed", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAMessage", name: "actionTwoPressed", object: nil)
//
let newIndexPath = NSIndexPath(forRow: todoItems.count, inSection: 0)
todoItems.append(todoItem)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
//scheduleLocalNotification(todoItem)
let notification:UILocalNotification = UILocalNotification()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "Notification \(todoItem.title)"
notification.fireDate = fixNotificationDate(todoItem.deadline)
notification.userInfo = ["note":todoItem.note, "title": todoItem.title]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
我已经完成了!但我不知道如何让'func showAMessage()'从'indexPath'中删除** todoItems **。我已经尝试过这样的东西:'todoItems.removeAtIndex(indexPath.row)'就像'commitEditingStyle',但没有结果。 –