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