gpt4 book ai didi

ios - 如何删除 UITableViewController 单元格中的 UILocalNotification?

转载 作者:行者123 更新时间:2023-11-29 03:24:09 25 4
gpt4 key购买 nike

我的问题是,每当我尝试从 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];
}

这是我的 TableView 代码:

- (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

最佳答案

像这样更改代码,我认为它会有所帮助...

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

关于ios - 如何删除 UITableViewController 单元格中的 UILocalNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com