2013-12-20 68 views
0

我的問題是,當我嘗試從TableView中刪除一個單元格。它似乎總是崩潰。問題是因爲它不能刪除一個UILocalNotification,它不知道從哪裏刪除它。似乎我需要一種方法來將整數分配給每個UILocalNotification或者其他的東西。我還沒有嘗試過,因爲我不知道該怎麼做。如何在UITableViewController單元中刪除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 [email protected]"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 
+0

如何使用cancelLocalNotification :.不更新scheduledLocalNotifications數組嗎? – rdelmar

+0

不能,當它保存爲數組時,不起作用。 – Ed3121577

+0

評論中的兩個「它的」是什麼? – rdelmar

回答

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]; 
} 
+0

然後,它給我: [UITableView的_endCellAnimationsWithContext:] + 11410 \t 5快速提醒0x0000000100004bdc - [GeneralTableViewController的tableView:commitEditingStyle:forRowAtIndexPath:] + 220 \t 6的UIKit 0x000000010071ee45 - [UITableView的animateDeletionOfRowWithCell:] + 85 \t 7的UIKit 0x00000001008700b8 - [UITableViewCell _swipeDeleteButtonPushed] + 60 \t 8 UIKit 0x0000000100653096 - – Ed3121577

+0

工作還是崩潰? – jailani

+0

刪除按鈕是可點擊的,但應用程序不會崩潰並且不會刪除。一旦我放置endUpdates它給了我這個:'NSInternalInconsistencyException',原因:'無效的更新:在0節的行數無效。 – Ed3121577

0

;

+0

我將在哪裏放置此代碼? – Ed3121577

+0

在方法中調用並且當您要刪除通知時調用 – Retro

+0

似乎不起作用。 – Ed3121577

相關問題