2017-03-08 6 views
1

私は、特定の日時に通知し、TableViewControllerに表示されるアプリケーションで作業しています。だから、私はテーブルに複数の通知があります。特定の通知を削除するにはどうすればよいですか? 通知を含む行を削除すると、削除されません。速報で通知を削除する3

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     let context = getContext() 
     if editingStyle == .delete { 
      // Delete the row from the data source 
      context.delete(names[(indexPath as NSIndexPath).row]) 

      let a = (names[(indexPath as NSIndexPath).row]) 

      let center = UNUserNotificationCenter.current() 
      let notifToDelete = a.name 

      center.getPendingNotificationRequests { (notifications) in 
        print(notifications) 
       for item in notifications { 
        if(item.identifier.contains(notifToDelete!)) 
{ 
    center.removePendingNotificationRequests(withIdentifiers: [item.identifier]) 

    } 
       } 
      } 

      do { 
       try context.save() 
      }catch { 
       let saveError = error as NSError 
       print(saveError) 
      } 
      names.remove(at: (indexPath as NSIndexPath).row) 

      tableView.deleteRows(at: [indexPath], with: .automatic) 

これは決して影響しません。通知は設定された時刻に実行されます。 (通知は別のView Controllerで設定されます)。

答えて

3

不要なコードブロックを削除します。

代わり
center.getPendingNotificationRequests { (notifications) in 
        print(notifications) 
       for item in notifications { 
        if(item.identifier.contains(notifToDelete!)) 
{ 
    center.removePendingNotificationRequests(withIdentifiers: [item.identifier]) 

    } 
       } 
      } 

、次の操作を行います。

center.removePendingNotificationRequests(withIdentifiers: [notifToDelete]) 
+0

はありがとうございました!!通知を削除しましたが、最後に保存した通知は削除されます。 –

+0

ああ!私は、通知用に設定した識別子が、削除する識別子と異なることを理解しました。ありがとう、トン! –

+0

心配はいりません。それが助けられたら私の答えを受け入れてください。 –

関連する問題