删除UITableView行时删除通知(Swift)

时间:2018-06-04 15:34:06

标签: ios swift uitableview push-notification uilocalnotification

我有一个包含多个UITableviews的视图控制器。我想从表视图中删除行以删除与此行关联的通知时,这是我的代码,但我收到错误Index out of range这里是我的项目的链接,如果有人想测试它:{{3 }}

我的代码:它没有简单的字符串,但是索引行我得到了错误。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {

        if tableView == ScheduleTableView{


            let defaults = UserDefaults.standard
            var myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
            var myarray2 = defaults.stringArray(forKey: "ScheduleTimeArray") ?? [String]()

          // Remove Row
            myarray.remove(at: indexPath.row)
            myarray2.remove(at: indexPath.row)
            defaults.set(myarray, forKey: "ScheduleArray")
            defaults.set(myarray2, forKey: "ScheduleTimeArray")
            ScheduleTableView.deleteRows(at: [indexPath], with: .fade)
            UserDefaults.standard.synchronize()



           // Remove Notification
            let center = UNUserNotificationCenter.current()
            let arrayToSearch = myarray[indexPath.row] as String
            center.getPendingNotificationRequests { (notifications) in
                for item in notifications {
                    if(item.identifier.contains(arrayToSearch)) {
                        center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
                    }
                }
            }


        }

        if tableView == GoalsTableView{


        }
 }

1 个答案:

答案 0 :(得分:1)

删除此行let arrayToSearch = myarray[indexPath.row] as String 并使用已删除的项目,因为您已经删除了myarray.remove(at: indexPath.row)

中的项目

以下是代码:

   func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

            if editingStyle == .delete {

            if tableView == ScheduleTableView{

               // Here is my code but it gives me an error, first you need to add a row with the add button
                let defaults = UserDefaults.standard
                var myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()

                print(myarray)
                let removedItem =  myarray.remove(at: indexPath.row)
                defaults.set(myarray, forKey: "ScheduleArray")
                ScheduleTableView.deleteRows(at: [indexPath], with: .fade)

                // Remove Notification
                let center = UNUserNotificationCenter.current()
                center.getPendingNotificationRequests { (notifications) in
                    for item in notifications {
                        if(item.identifier.contains(removedItem)) {
                            center.removePendingNotificationRequests(withIdentifiers: [item.identifier])
                        }
                    }
                }


            }

            if tableView == GoalsTableView{



            }
        }