Ios 删除tableview中的所有本地通知和单元格
我正在寻找一种方法,用一个按钮删除我的tableView中的所有Ios 删除tableview中的所有本地通知和单元格,ios,objective-c,uitableview,uilocalnotification,Ios,Objective C,Uitableview,Uilocalnotification,我正在寻找一种方法,用一个按钮删除我的tableView中的所有localNotifications。 我试过玩cancelAllNotifications和[array removeAllObject]但那没有成功。 我确实有幻灯片删除功能一对一工作 需要知道的是,每个部分都有两行localNotifications,如下所示 cellForRowAtIndexPath: UITableViewCell *cell = [tableView dequeueReusableCellWit
localNotifications
。
我试过玩cancelAllNotifications
和[array removeAllObject]代码>但那没有成功。
我确实有幻灯片删除功能一对一工作
需要知道的是,每个部分都有两行localNotifications
,如下所示
cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
int rowIndex = (indexPath.section*2) + indexPath.row;
// Get list of local notifications
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *localNotification = [localNotifications objectAtIndex:rowIndex];
// Display notification info
[cell.textLabel setText:[localNotification.alertAction description]];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
return cell;
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
int rowIndex = (indexPath.section*2); //get the 2 items of the section
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notify = [localNotifications objectAtIndex:rowIndex];
[[UIApplication sharedApplication] cancelLocalNotification:notify];
notify = [localNotifications objectAtIndex:rowIndex+1];
[[UIApplication sharedApplication] cancelLocalNotification:notify];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
canEditRowAtIndexPath
return YES;
[self.tableView reloadData];
提交方式:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
int rowIndex = (indexPath.section*2) + indexPath.row;
// Get list of local notifications
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *localNotification = [localNotifications objectAtIndex:rowIndex];
// Display notification info
[cell.textLabel setText:[localNotification.alertAction description]];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
return cell;
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
int rowIndex = (indexPath.section*2); //get the 2 items of the section
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notify = [localNotifications objectAtIndex:rowIndex];
[[UIApplication sharedApplication] cancelLocalNotification:notify];
notify = [localNotifications objectAtIndex:rowIndex+1];
[[UIApplication sharedApplication] cancelLocalNotification:notify];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
任何人都知道如何做到这一点。从数据源数组中删除所有对象后是否重新加载了表视图
您应该在按钮的操作中执行此操作
- (void)clearAllButtonTapped:(UIButton *)clearAllButton
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[theDataArray removeAllObjects];
[theTableView reloadData];
}
我重新加载了表视图:[self.tableView-reloadData]在caneditrowatinexpath
方法中的code>。现在的问题是,我无法在removeAllObjects中设置数组,即使我在.h文件中声明了notifications数组。在CaneditRowatingIndexPath
中重新加载表视图不是一个好方法,该方法只是为了知道行是否可以编辑。执行其他操作可能会导致一些潜在问题。而且我不太确定我无法在removeAllObjects中设置数组。您的意思是要在删除所有现有通知后添加一些新的本地通知吗?如果是这样,您应该使用[[UIApplication sharedApplication]scheduleLocalNotification:localNotification]
。简单地添加到数组不应该起作用。对于错误的解释,很抱歉。按下按钮后,名为localNotifications的localNotifications
数组必须为空,对吗?正如您在代码中看到的,数组localnotifications是在cellForRowAtIndexPath和committeditingstyle中生成的。所以我不能在你的void方法中得到它。那么你就不需要删除allobjects
。因为每次需要时都会从[[UIApplication sharedApplication]scheduledLocalNotifications]
获取localNotifications
数组。在您取消所有本地通知后,[[UIApplication sharedApplication]ScheduledLocalNotification]
方法将自动返回nil(或空数组?我不确定)。