2011-11-04 23 views
0

這是我的錯誤:localNotification /的UITableView /刪除行 - >錯誤「無效的行數」

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我知道這意味着什麼,但我不能找到我的代碼中的錯誤。我知道我只能使用NSMutableArry。不是一個正常的NSArray.This是我認爲的觀點...

在我的h。 File: NSMutableArray * notifArray,IBOutlet UITableView * myTable;

CODE:

​​

CODE:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 

    NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    UILocalNotification *notif = [_notifArray objectAtIndex:indexPath.row]; 
    <...> 

CODE:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     // If row is deleted, remove it from the list. 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      [notifArray removeObjectAtIndex:indexPath.row]; 
      [self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

      [myTable reloadData]; 
     } 
    } 

CODE:

- (IBAction) scheduleAlarm:(id) sender { 
    [eventText resignFirstResponder]; 

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    // Get the current date 
    NSDate *pickerDate = [self.datePicker date]; 

    // Break the date up into components 
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                fromDate:pickerDate]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                fromDate:pickerDate]; 

    // Set up the fire time 
    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setDay:[dateComponents day]]; 
    [dateComps setMonth:[dateComponents month]]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setHour:[timeComponents hour]]; 
    // Notification will fire in one minute 
    [dateComps setMinute:[timeComponents minute]]; 
    [dateComps setSecond:[timeComponents second]]; 
    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 
    [dateComps release]; 

    localNotification = [[UILocalNotification alloc] init]; 
    if (localNotification == nil) 
     return; 
    localNotification.fireDate = itemDate; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

    // Notification details 
    localNotification.alertBody = [eventText text]; 
    // Set the action button 
    localNotification.alertAction = @"View"; 

    localNotification.soundName = UILocalNotificationDefaultSoundName; 
    localNotification.applicationIconBadgeNumber = 1; 

    // Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"]; 
    localNotification.userInfo = infoDict; 

    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
    [localNotification release]; 


    [self.myTable reloadData]; 
} 

如果我改變此行的NSMutabelArray比我得到一個錯誤了。 「不兼容的指針類型初始化‘的NSMutableArray’類型的表達式‘的NSArray *’

---> NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

所以我能做些什麼,這是可以刪除包括localNotification行?

回答

1

非常感謝!

我覺得我的問題起初是一個錯誤的代碼;-)然後我忘了連續顯示的通知是兩件事!所以我要刪除起初所述通知和第二次theRow在我的tableView ;-)

這裏是我的代碼 - 隨時;-)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
// If row is deleted, remove it from the list. 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
      { 

      // DELETE theNotification defined in (UITableViewCell *)tableView:{} 
      [[UIApplication sharedApplication] cancelLocalNotification:notifcation]; 

      // DELETE theRow 
      [notificationsArray removeObjectAtIndex:indexPath.row]; 
      [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 

      [tableView reloadData]; 
    }  

}

YEEAARRR我好開心; - )無論如何,我真的不知道是否有新的東西;-) - 所以如果有人有更好的方式隨時糾正我:-)

0

關於initilisation你可以創建可變數組是這樣的:

NSMutableArray *_notifArray = [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]]; 

而且你可能需要保留它,以及

關於線缺失,我想知道reloadData的調用。我不認爲這是必要的,因爲上一行DeleteRows ...導致表視圖的更新,我甚至不知道它是否可能是您的消息的原因。當然,它是在DeleteRows之後調用的,但我們沒有真正的方法知道這是如何排序的,並且如果在DeleteRows完成之前重新加載查詢numberOfRows,那麼它可能會導致您的消息。

希望這會有所幫助。

+0

嗨,先謝謝你幫助我。 所以在測試了一些代碼之後,我的問題現在成了我的一個念頭。或者我的代碼更好知道;-) 如果我點擊編輯並刪除一行,該行就會淡出。這似乎是行刪除。但重新啓動應用程序後,舊數據的行仍然存在;-( 一個問題已解決 - 一個新的在我的代碼* arrrghhh – webschnecke

+0

' - (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 如果(editingStyle == UITableViewCellEditingStyleDelete){// 從表中刪除該對象 [notificationsArray removeObjectAtIndex:indexPath.row]; [TV deleteRowsAtIndexPaths:[NSArray的arrayWithObject:indexPath] withRowAnimation :UITableViewRowAnimationLeft]; } ' – webschnecke

+0

我認爲表視圖比通知更多,也許你可以用這個線程完成解決方案: http://stackoverflow.com/questions/3158264/cancel-uilocalnotification – gregory