這是我的錯誤: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行?
嗨,先謝謝你幫助我。 所以在測試了一些代碼之後,我的問題現在成了我的一個念頭。或者我的代碼更好知道;-) 如果我點擊編輯並刪除一行,該行就會淡出。這似乎是行刪除。但重新啓動應用程序後,舊數據的行仍然存在;-( 一個問題已解決 - 一個新的在我的代碼* arrrghhh – webschnecke
' - (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 如果(editingStyle == UITableViewCellEditingStyleDelete){// 從表中刪除該對象 [notificationsArray removeObjectAtIndex:indexPath.row]; [TV deleteRowsAtIndexPaths:[NSArray的arrayWithObject:indexPath] withRowAnimation :UITableViewRowAnimationLeft]; } ' – webschnecke
我認爲表視圖比通知更多,也許你可以用這個線程完成解決方案: http://stackoverflow.com/questions/3158264/cancel-uilocalnotification – gregory