localNotification / uiTableView / delete row -> ERROR "invalid number of rows"



这是我的错误:

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

我知道这意味着什么,但我在代码中找不到我的错误。我知道我只能使用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的行呢?

非常感谢

我认为我的问题最初是一个错误的代码;-(第二次我忘记了连续显示的通知是两件事!因此,我必须先删除Notification,然后再删除表中的Row View;-(

这是我的代码-请放心;-(

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

}

是的,我很高兴;-(无论如何,我真的是个新手因此,如果有人有更好的方法,请随时纠正我:-(

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

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

你可能也需要保留它。

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

希望这能有所帮助。

相关内容

最新更新