-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)の行により、アプリが 2 回実行されたようにクラッシュします ...

ご協力ありがとうございました ...

Github リンク

プロジェクトをプルして試してみたい場合は...

+ボタンで成績を作成する必要があります。その後、テーブルビューでアイテムを削除してみてください。

成績を追加する場合、最初の TextField はstring、2 番目はdouble値、3 番目もdouble値です。

4

4 に答える 4

0

問題は、行ではなくセクションを削除していることです。

[notes removeObjectAtIndex:indexPath.section];する必要があります[notes removeObjectAtIndex:indexPath.row];

また[_gradesTV deleteSections、する必要があります[_gradesTV deleteRow...

注: どちらも役に立たないため、orを使用する必要があります。私はただ使うと言うでしょう[_gradesTV deleteRow...[_gradesTV reloadData];[_gradesTV deleteRow...

また、何のためにあるのかわからbeginUpdatesないendUpdates。不要なものを取り除きます。

于 2015-02-18T13:47:26.483 に答える
0

わかりました、私は間違いがどこから来たのかを理解しました。それは、同じ MutableArray を使用してセクション数を取得しなかったことです...

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

古いメソッドには、コードをテストするために使用していた古い NSMutableArray がありました...

初心者すぎて自分で気付かなかったのに、みんなありがとう....

于 2015-02-18T16:28:01.977 に答える
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

Firstly remove UITableView's beginUpdates and endUpdates

Secondly remove UITableView's reloadData as no need when using UITableViewCellEditingStyleDelete and UITableViewCellEditingStyleInsert

Now your code will be :

- (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 に答える