0
votes

I'm trying to build an alarm clock using UILocalNotification. I am able to schedule alarms and populate them in a UITableView.. Notification alerts are working fine, and I can easily cancel all scheduled notifications. My issues are:

  1. How can i delete a specific local notification from the table?
  2. How can i keep notifications from disappearing from the table after they have been fired like the apple alarm clock when they black out after being fired.

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
    
    var notificationArray:NSArray = UIApplication.sharedApplication().scheduledLocalNotifications
    var notification:UILocalNotification = notificationArray.objectAtIndex(indexPath.row) as UILocalNotification
    
    UIApplication.sharedApplication().cancelLocalNotification(notification)
    
    alarmTable.reloadData()
    
       }
    }
    
2

2 Answers

0
votes

If you want to delete a row, use

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([ indexPathRowToDelete ], withRowAnimation: UITableViewRowAnimation.Fade)
tableViewMain.endUpdates()

You do not need to use reloadData, since the function will take care of this.

However, if you want to make an entry inactive, e.g. not delete it, use a variable for each data entry which shows whether it is active or not. Check the flag in the cellForRowAtIndexPath method. You then can set the flag accordingly and call reloadRowsAtIndexPaths.

Sidenote: accessing UIApplication.sharedApplication() to retrieve data is not such a good idea. It is better to pass the data to the data source of your table view.

0
votes

I found a solution to my problem. From what i understood is while removing the local notification and reloading the table, it would crash because it assumes the row of the deleted notification is still there. So what i did was that i added a command to delete the row under the remove local notification command. And that solved my problem.