【问题标题】:Remove Notification when UITableView row is deleted (Swift)删除 UITableView 行时删除通知(Swift)
【发布时间】:2018-06-04 15:34:06
【问题描述】:

我有一个带有多个 UITableviews 的视图控制器。我想在从 tableview 中删除一行时删除与该行关联的通知,这是我的代码,但我收到错误 Index out of range 如果有人想测试它,这是我的项目的链接:https://files.fm/f/nwx8e7je

我的代码:它可以在没有简单字符串的情况下工作,但使用索引行时出现错误。

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{


        }
 }

【问题讨论】:

    标签: ios swift uitableview push-notification uilocalnotification


    【解决方案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{
    
    
    
                }
            }
    

    【讨论】:

    • 工作,谢谢!
    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2021-08-02
    • 2017-03-08
    • 1970-01-01
    • 2017-01-28
    • 2023-04-05
    相关资源
    最近更新 更多