-1

这是我的问题:

我有一个NSMutableArray *notes和我的来源UITableView* _gradesTV

我在- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath这里添加了方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"%ld", (long)indexPath.section);
        NSLog(@"%@", notes);




        [_gradesTV beginUpdates];

        [notes removeObjectAtIndex:indexPath.section];

        [_gradesTV deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                 withRowAnimation:UITableViewRowAnimationFade];

        [_gradesTV endUpdates];




        NSLog(@"%@", notes);
    }

[_gradesTV reloadData];

}

但是当我删除一个元素时,我得到了这个错误:

*** Terminating app due to uncaught exception 'NSRangeException',
 reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

但是我在 MutableArray 上制作的 NSLog 向我展示了 3 个元素......

removeObjectAtIndex注释上的行(MutableArray)使我的应用程序崩溃,就像它被执行了两次一样......

谢谢您的帮助 ...

Github 链接

如果你想拉项目并尝试......

您必须使用 + 按钮创建成绩,然后您可以尝试删除我的表格视图中的项目。

添加成绩时,第一个 TextField 是 a string,第二个是一个double值,第三个也是一个double值。

4

4 回答 4

0

首先删除UITableView's beginUpdatesendUpdates

其次UITableView's reloadData,在使用时不需要 删除,UITableViewCellEditingStyleDelete并且UITableViewCellEditingStyleInsert

现在您的代码将是:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"Current Section : %ld", (long)indexPath.section);
        NSLog(@"Before : %@", notes);

        [notes removeObjectAtIndex:indexPath.section];

        // Either delete some rows within a section (leaving at least one) or the entire section.
        id sectionDataSource = [notes objectAtIndex:indexPath.section];
        if ([sectionDataSource count] > 0)
        {
            // Section is not yet empty, so delete only the current row.
            [_gradesTV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            // Section is now completely empty, so delete the entire section.
            [_gradesTV deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                     withRowAnimation:UITableViewRowAnimationFade];
        }

        NSLog(@"After : %@", notes);
    }
}
于 2015-02-18T13:46:37.170 回答
0

问题是您正在删除部分而不是行。

[notes removeObjectAtIndex:indexPath.section];应该[notes removeObjectAtIndex:indexPath.row];

[_gradesTV deleteSections应该是[_gradesTV deleteRow...

注意: 你应该使用[_gradesTV deleteRow...or[_gradesTV reloadData];因为两者都没用。我会说只是使用[_gradesTV deleteRow...

也不确定beginUpdates&endUpdates是为了什么。删除不必要的东西。

于 2015-02-18T13:47:26.483 回答
0

尝试使用以下代码..

- (NSInteger)numberOfSections {
    return [notes count];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSLog(@"%ld", (long)indexPath.section);
        NSLog(@"%@", notes);
        [notes removeObjectAtIndex:indexPath.section];
        NSLog(@"%@", notes);
    }

[_gradesTV reloadData];

}
于 2015-02-18T13:44:36.033 回答
0

好的,我想出了我的错误从哪里来,只是我没有使用相同的 MutableArray 来获取部分的数量......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [notes count];
}

旧方法有一个旧的 NSMutableArray 我用来测试我的代码......

谢谢大家,即使我自己太菜鸟也没有注意到......

于 2015-02-18T16:28:01.977 回答