代码之家  ›  专栏  ›  技术社区  ›  CoderJ13

删除uitableview行时删除通知(swift)

  •  0
  • CoderJ13  · 技术社区  · 7 年前

    我有一个具有多个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{
    
    
            }
     }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Abdelahad Darwish    7 年前

    删除此行 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{
    
    
    
                }
            }