2014-07-27 38 views
0

我正在尋找一種方法來刪除我的tableView中的所有localNotifications,並帶有一個按鈕。 我嘗試過玩cancelAllNotifications[array removeAllObject];,但那並沒有奏效。 我有滑動刪除功能再見一個工作。刪除tableview中的所有localnotifications和單元格

需要知道的是,每個部分都有2行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; 

canEditRowAtIndexPath

return YES;  
    [self.tableView reloadData]; 

commitEditingStyle:

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

任何人都有一個想法如何做到這一點。

回答

1

從數據源數組中刪除所有對象後,是否重新加載了表視圖?

你應該在你的按鈕的動作

- (void)clearAllButtonTapped:(UIButton *)clearAllButton 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    [theDataArray removeAllObjects]; 
    [theTableView reloadData]; 
} 
+0

我重新裝表視圖做到這一點:'[self.tableView reloadData];'的'canEditRowAtIndexPath'方法。現在的問題是我無法在removeAllObjects中設置數組,即使我在.h文件中聲明通知數組。 – SwingerDinger

+0

在'canEditRowAtIndexPath'中重新加載表視圖並不是一種好方法,該方法只是爲了瞭解某行是否可以編輯而設計的。其他事情可能會導致一些潛在的問題。 –

+0

我不太確定'我無法在removeAllObjects中設置數組。你的意思是你想刪除所有現有的通知後添加一些新的本地通知?如果是這樣,你應該使用'[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]'。只需添加到陣列不應該工作。 –

相關問題