0

这是我的错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于该节中包含的行数更新前的节 (2),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出去)。'

我知道这意味着什么,但我在代码中找不到我的错误。我知道我只能使用 NSMutableArry。不是正常的 NSArray。这是我认为的重点......

在我的 H. 文件:NSMutableArray *notifArray, IBOutlet UITableView *myTable;

代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];

    }

代码:

// 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];
    <...>

代码:

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

代码:

- (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,也会出现错误。“使用“NSArray*”类型的表达式初始化“NSMUtableArray”的不兼容指针类型

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

那么我该怎么办,可以删除包含 localNotification 的行?

4

2 回答 2

1

多谢!!!

我认为我的问题起初是一个错误的代码;-) 然后我忘记了连续显示的通知是两件事!所以我必须先删除 theNotification ,然后再删除 tableView 中的 theRow ;-)

这是我的代码 - 随意;-)

- (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 我很高兴 ;-) 无论如何,我在编码方面真的很新 ;-) - 所以如果有人有更好的方法,请随时纠正我 :-)

于 2011-11-05T14:30:26.527 回答
0

关于初始化,您可以像这样创建可变数组:

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

您可能还需要保留它。

关于行删除,我想知道对 reloadData 的调用。我认为没有必要,因为上一行 DeleteRows... 导致表视图的更新,我什至想知道这是否可能是您的消息的原因。当然它是在 DeleteRows 之后调用的,但我们无法真正知道这一切是如何排序的,如果在 DeleteRows 完成之前重新加载查询 numberOfRows,那么它可能会导致您的消息。

希望这可以帮助。

于 2011-11-04T13:01:07.780 回答