localNotification/uiTableView/删除行->错误“行数无效”

发布于 2024-12-13 13:32:47 字数 4374 浏览 0 评论 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 的行?

This is my ERROR:

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).'

I know what that means but I cant find my mistake in code. I know that I have to use NSMutableArry, only. Not a normale NSArray.This is the Point I think...

In my h. File:
NSMutableArray *notifArray, IBOutlet UITableView *myTable;

CODE:

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

    }

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

If I change this line to a NSMutabelArray than I got an error, too. "Incompatible pointer types initializing "NSMUtableArray" with an expression of type "NSArray*"

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

So what can I do, that it is possible to delete the row including a localNotification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-12-20 13:32:47

非常感谢!!!

我认为我的问题首先是一个错误的代码;-),然后我忘记了连续显示的通知有两件事!所以我必须首先删除我的 tableView 中的 theNotification 和第二次 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 我很高兴 ;-) 无论如何,我在编码方面真的很新 ;-) - 所以如果有人有更好的方法请随时纠正我:-)

THANKS A LOT!!!

I think my Problem was at first a wrong code ;-) and at second I forgot that a notification displyed in a row are TWO THINGS! So I have to delete AT FIRST theNotification and second time theRow in my tableView ;-)

Here is my code - feel free ;-)

- (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 Im so happy ;-) Im really new in coading anyway ;-) - So If anybody has a better way feel free to correct me :-)

浅黛梨妆こ 2024-12-20 13:32:47

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

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

并且您可能还需要保留它。

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

希望这有帮助。

Regarding the initilisation you can create the mutable array like this :

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

And you probably need to retain it as well.

Regarding the line deletion, I wonder about the call to reloadData. I don't think it is necessary, since the previous line DeleteRows... causes the update of the table view, and I even wonder if it could be the cause of your message. Of course it is called after DeleteRows but we have no real way of knowing how this is all sequenced, and if the reload queries numberOfRows before the DeleteRows is completed, then it can cause your message.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文