我的問題是,當我嘗試從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
如何使用cancelLocalNotification :.不更新scheduledLocalNotifications數組嗎? – rdelmar
不能,當它保存爲數組時,不起作用。 – Ed3121577
評論中的兩個「它的」是什麼? – rdelmar