1

NSUserDefaults から読み取られる可変配列が取り込まれた UITableView があります。ユーザーが存在するアイテムの一部を削除できるようにしたい。以下は編集方法に固有のコードで、ファイル全体は次のとおりです: http://pastebin.com/LmZMWBN9

「編集」をクリックして項目を削除すると、アプリケーションがクラッシュして元に戻ります:

キャッチされていない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: '-[__NSCFArray removeObjectAtIndex:]: 変更メソッドが不変オブジェクトに送信されました'

私の具体的な質問は、ここで間違っていることは何ですか?

 // Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    // edit list of names 
    if ([listOfNames count] >= 1) {
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [listOfNames removeObjectAtIndex:[indexPath row]];

       // write updated listofnames to nsuserdefaults
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

         [[NSUserDefaults standardUserDefaults] setObject:listOfNames forKey:@"My Key"];

        [defaults synchronize];

        if ([listOfNames count] == 0) {
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        [tableView endUpdates];
    }


}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}
4

1 に答える 1

3

listOfNamesインスタンス変数は不変であるため、インスタンス変数からオブジェクトを削除することはできません。

変化する:

listOfNames = [[defaults objectForKey:@"My Key"] copy];

listOfNames = [[defaults objectForKey:@"My Key"] mutableCopy];

あなた-viewWillAppear:viewDidLoadメソッドで。

于 2012-05-22T11:57:42.193 に答える