如何在UITableViewController单元格中删除UILocalNotification?

时间:2013-12-20 05:25:44

标签: ios notifications uilocalnotification

我的问题是,当我尝试从TableView中删除一个单元格时。它似乎总是崩溃。问题是因为它无法删除UILocalNotification,它不知道从哪里删除它。似乎我需要一种方法来为每个UILocalNotification或其他东西分配整数。我还没试过,因为我不知道怎么做。

这就是我使用UILocalNotifications的方式:

-(IBAction)threehour:(id)sender{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *remind = [defaults objectForKey:@"remind"];

NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10800];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = alertTime;
localNotification.alertBody = remind;
localNotification.soundName =@"alarm.mp3";
localNotification.timeZone = [NSTimeZone defaultTimeZone];


NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
localNotification.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

[self performSegueWithIdentifier:@"AlarmTimeBack" sender:sender];
}

这是我的表格视图代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Get list of local notifications
        NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
        UILocalNotification *localNotification = [localNotifications objectAtIndex:indexPath.row];

        // Display notification info
        [cell.textLabel setText:localNotification.alertBody];
        [cell.detailTextLabel setText:[localNotification.fireDate description]];

        return cell;
    }

    - (void)reloadTable
    {
        [self.tableView reloadData];
    }

    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.

        return YES;

        [self.tableView reloadData];
    }

    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }

        [tableView reloadData];
    }

    // Swipe ot delete action.
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleDelete;
    }

    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
          if (self.searchDisplayController.isActive) {
            [self performSegueWithIdentifier:@"ShowDetail" sender:self];
        }
    }

@end

2 个答案:

答案 0 :(得分:1)

更改像这样的代码,我认为......

 if (editingStyle == UITableViewCellEditingStyleDelete)
 {
     // Delete the row from the data source

     NSArray *localNotifications = [[UIApplication sharedApplication]  scheduledLocalNotifications];
     UILocalNotification *notify = [localNotifications objectAtIndex:indexPath.row];
     [[UIApplication sharedApplication] cancelLocalNotification:notify];             
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }

答案 1 :(得分:0)

NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
  if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
     notificationToCancel=aNotif;
     break;
  }
}
[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel]